Skip to content

Commit

Permalink
fix: s3 directory 설정 수정 (#521)
Browse files Browse the repository at this point in the history
* feat: s3 이미지 업로드 기능 추가

* fix: 이미지 업로드 관련 빈이 로컬에서 로드되지 않도록 변경

* fix: url 수정

* fix: s3 directory 수정
  • Loading branch information
poi1649 authored Oct 19, 2023
1 parent 5904860 commit 737f5a2
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,43 @@
@Service
public class ImageResourceService {

private final String BUCKET_NAME;
private final String CLOUD_FRONT_DOMAIN_NAME;
private final S3Client S3_CLIENT;
private final String bucketName;
private final String cloudFrontDomainName;
private final S3Client s3Client;
private final String directoryName;

public ImageResourceService(
@Value("${aws.region}") String region,
@Value("${aws.s3.bucket-name}") String bucketName,
@Value("${aws.cloud-front-domain}") String cloudFrontDomainName
@Value("${aws.cloud-front-domain}") String cloudFrontDomainName,
@Value("${aws.s3.directory-name}") String directoryName
) {
BUCKET_NAME = bucketName;
CLOUD_FRONT_DOMAIN_NAME = cloudFrontDomainName;
S3_CLIENT = S3Client.builder()
.region(Region.of(region))
.build();
this.bucketName = bucketName;
this.cloudFrontDomainName = cloudFrontDomainName;
this.s3Client = S3Client.builder()
.region(Region.of(region))
.build();
this.directoryName = directoryName;
}

public String uploadImage(MultipartFile image) {
String key = createKey(ImageExtension.from(image.getOriginalFilename()));
try {
S3_CLIENT.putObject(
s3Client.putObject(
PutObjectRequest.builder()
.bucket(BUCKET_NAME)
.bucket(bucketName)
.key(key)
.build(),
RequestBody.fromBytes(image.getBytes())
);
return CLOUD_FRONT_DOMAIN_NAME + "/" + key;
return cloudFrontDomainName + "/" + key;
} catch (IOException e) {
throw new ImageToBytesException(e);
}
}

private String createKey(ImageExtension extension) {
return UUID.randomUUID() + extension.toString();
return directoryName + "/" + UUID.randomUUID() + extension.toString();
}

enum ImageExtension {
Expand Down

0 comments on commit 737f5a2

Please sign in to comment.