forked from codesquad-members-2023/issue-tracker-max
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'be-w4' into be/feature/#174-issue-remove
- Loading branch information
Showing
3 changed files
with
54 additions
and
39 deletions.
There are no files selected for viewing
51 changes: 13 additions & 38 deletions
51
backend/src/main/java/kr/codesquad/issuetracker/application/S3Service.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,27 @@ | ||
package kr.codesquad.issuetracker.application; | ||
|
||
import java.net.URLDecoder; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
|
||
import kr.codesquad.issuetracker.application.s3.S3Uploader; | ||
import kr.codesquad.issuetracker.domain.ImageFile; | ||
import kr.codesquad.issuetracker.infrastructure.config.AwsProperties; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class S3Service { | ||
|
||
private static final String UPLOADED_IMAGES_DIR = "public/uploaded-images/"; | ||
private static final String PROFILE_IMAGES_DIR = "public/profile-images/"; | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
private final String bucket; | ||
|
||
public S3Service(AmazonS3Client amazonS3Client, AwsProperties awsProperties) { | ||
this.amazonS3Client = amazonS3Client; | ||
this.bucket = awsProperties.getS3().getBucket(); | ||
} | ||
|
||
public String uploadImage(MultipartFile file) { | ||
ImageFile imageFile = ImageFile.from(file); | ||
|
||
// 버킷에 저장할 파일 이름 생성 | ||
String fileName = UPLOADED_IMAGES_DIR + imageFile.getRandomName(); | ||
private static final String UPLOADED_IMAGES_DIR = "public/uploaded-images/"; | ||
|
||
uploadImageToS3(imageFile, fileName); | ||
return getObjectUri(fileName); | ||
} | ||
private final S3Uploader s3Uploader; | ||
|
||
private void uploadImageToS3(ImageFile imageFile, String fileName) { | ||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentType(imageFile.getContentType()); | ||
metadata.setContentLength(imageFile.getFileSize()); | ||
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, imageFile.getImageInputStream(), metadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
} | ||
@Transactional | ||
public String uploadImage(MultipartFile file) { | ||
ImageFile imageFile = ImageFile.from(file); | ||
|
||
private String getObjectUri(String fileName) { | ||
return URLDecoder.decode(amazonS3Client.getUrl(bucket, fileName).toString(), StandardCharsets.UTF_8); | ||
} | ||
// 버킷에 저장할 파일 이름 생성 | ||
String fileName = UPLOADED_IMAGES_DIR + imageFile.getRandomName(); | ||
return s3Uploader.uploadImageToS3(imageFile, fileName); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/kr/codesquad/issuetracker/application/s3/S3Uploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package kr.codesquad.issuetracker.application.s3; | ||
|
||
import java.net.URLDecoder; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
|
||
import kr.codesquad.issuetracker.domain.ImageFile; | ||
import kr.codesquad.issuetracker.infrastructure.config.AwsProperties; | ||
|
||
@Component | ||
public class S3Uploader { | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
private final String bucket; | ||
|
||
public S3Uploader(AmazonS3Client amazonS3Client, AwsProperties awsProperties) { | ||
this.amazonS3Client = amazonS3Client; | ||
this.bucket = awsProperties.getS3().getBucket(); | ||
} | ||
|
||
public String uploadImageToS3(ImageFile imageFile, String fileName) { | ||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentType(imageFile.getContentType()); | ||
metadata.setContentLength(imageFile.getFileSize()); | ||
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, imageFile.getImageInputStream(), metadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
return getObjectUri(fileName); | ||
} | ||
|
||
private String getObjectUri(String fileName) { | ||
return URLDecoder.decode(amazonS3Client.getUrl(bucket, fileName).toString(), StandardCharsets.UTF_8); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters