Skip to content

Commit

Permalink
Merge branch 'be-w4' into be/feature/#174-issue-remove
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdqls1200 committed Aug 18, 2023
2 parents 7284ca0 + a01bcf8 commit 18ef825
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers)
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("http:https://issue-tracker-8.s3-website.ap-northeast-2.amazonaws.com", "http:https://localhost:5173")
.allowedOrigins("http:https://issue-tracker-8.s3-website.ap-northeast-2.amazonaws.com", "http:https://localhost:5173",
"https://fe-w4.d2kit8rai80g4y.amplifyapp.com/")
.allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "DELETE", "PATCH");
}

Expand Down

0 comments on commit 18ef825

Please sign in to comment.