νλ‘μ νΈλ₯Ό μ§ννλ©° μ¬μ©μμ λΉμμ΄ μμ± νμΌμ μ μ₯ν΄μΌ νλ κΈ°λ₯μ΄ μΆκ°λμ΄.
μ΄λ₯Ό μν΄ AWS S3μ νμΌμ μ
λ‘λ λ° κ΄λ¦¬νκΈ°λ‘ κ²°μ νλ€.
μ΄λ² κΈμμλ AWS S3 λ²ν· μμ±λΆν° Spring Bootμμ νμΌ μ λ‘λ μ½λ ꡬνκΉμ§ λ€λ£° μμ μ΄λ€.
β AWS κ³μ
β Spring Boot νλ‘μ νΈ
μ¬μ©μ ν΄λ¦
μ¬μ©μ μμ± ν΄λ¦
AmazonS3FullAccess κΆν λΆμ¬


π μ€μ:
μμΈμ€ ν€μλΉλ° μμΈμ€ ν€λ ν λ²λ§ μ 곡λλ―λ‘CSV νμΌ λ€μ΄λ‘λλλ λ³λλ‘ μμ νκ² λ³΄κ΄ν΄μΌ νλ€.
λ²ν· μμ± ν΄λ¦

β οΈ μ€λ¬΄μμλ λͺ¨λ νΌλΈλ¦ μ‘μΈμ€λ₯Ό μ°¨λ¨νλ κ²μ΄ μΌλ°μ μ΄μ§λ§, ν μ€νΈ νκ²½μμλ μΌλΆ ν΄μ ν μ μλ€.
build.gradleμ AWS S3 μμ‘΄μ± μΆκ°// AWS S3
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.510'
application.yml μ€μ μΆκ°aws:
access-key-id: YOUR_AWS_ACCESS_KEY
secret-access-key: YOUR_AWS_SECRET_KEY
s3:
bucket-name: λ²ν· μ΄λ¦
region: ap-northeast-2
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AmazonS3Config {
@Value("${aws.access-key-id}")
private String awsAccessKey;
@Value("${aws.secret-access-key}")
private String awsSecretKey;
@Value("${aws.s3.region}")
private String region;
@Bean
public AmazonS3 amazonS3() {
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
return AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}
}
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@Service
public class S3Service {
private final AmazonS3 amazonS3;
@Value("${aws.s3.bucket-name}")
private String bucketName;
public S3Service(AmazonS3 amazonS3) {
this.amazonS3 = amazonS3;
}
public String uploadFile(MultipartFile file) {
String fileName = "uploads/" + System.currentTimeMillis() + "_" + file.getOriginalFilename();
try {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());
metadata.setContentType(file.getContentType());
amazonS3.putObject(bucketName, fileName, file.getInputStream(), metadata);
return amazonS3.getUrl(bucketName, fileName).toString();
} catch (IOException e) {
throw new RuntimeException("νμΌ μ
λ‘λμ μ€ν¨νμ΅λλ€.", e);
}
}
}
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/files")
public class S3Controller {
private final S3Service s3Service;
public S3Controller(S3Service s3Service) {
this.s3Service = s3Service;
}
@PostMapping("/upload")
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
String fileUrl = s3Service.uploadFile(file);
return ResponseEntity.ok(fileUrl);
}
}
μ΄μ Spring Bootμμ AWS S3μ νμΌμ μ
λ‘λν μ μμ΅λλ€! π₯³
μΆκ°μ μΌλ‘ μμ λ° μ‘°ν κΈ°λ₯μ ꡬννλ©΄ λμ± μλ²½ν κΈ°λ₯μ λ§λ€ μ μμ΅λλ€.