반응형
1. pom.xml 파일에 추가합니다.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.808</version>
</dependency>
2. AWS 자격 증명을 구성합니다.
import com.amazonaws.auth.AWSCredentials;
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 AWSConfig {
@Value("${aws.access.key}")
private String awsAccessKey;
@Value("${aws.secret.key}")
private String awsSecretKey;
@Bean
public AmazonS3 s3client() {
AWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
return AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();
}
}
3. S3에 파일 업로드
import com.amazonaws.services.s3.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@Service
public class S3Service {
@Autowired
private AmazonS3 s3client;
public void uploadFileToS3(MultipartFile file, String bucketName, String fileName) throws IOException {
File fileToUpload = convertMultiPartToFile(file);
s3client.putObject(new PutObjectRequest(bucketName, fileName, fileToUpload));
}
private File convertMultiPartToFile(MultipartFile file) throws IOException {
File convFile = new File(file.getOriginalFilename());
file.transferTo(convFile);
return convFile;
}
}
반응형
'Spring Boot' 카테고리의 다른 글
[Springboot + Vimeo] vimeo 파일 다운로드 구현 (0) | 2023.02.08 |
---|---|
[Springboot + Vimeo] vimeo 파일 업로드 구현 (0) | 2023.02.08 |
[Google API + Springboot] Gmail 록록 취득하기 (0) | 2023.02.08 |
[Springboot] Instagram 사진 올리기 연동 구현(인스타그램 + java) (0) | 2023.02.08 |
[Springboot] Instagram 로그인 연동 구현(인스타그램 + java) (0) | 2023.02.08 |