Skip to content

Commit

Permalink
chore: S3 관련 설정 파일 추가 (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeong-hyeok committed Oct 21, 2023
1 parent d833573 commit c7fc832
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/main/java/com/project/mapdagu/common/S3/FileService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.project.mapdagu.common.S3;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.project.mapdagu.error.exception.custom.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import static com.project.mapdagu.error.ErrorCode.INVALID_FILE_UPLOAD;

@Slf4j
@Service
@RequiredArgsConstructor
public class FileService {

private final AmazonS3Client amazonS3Client;

@Value("${cloud.aws.s3.bucket.name}")
private String bucketName;

@Value("${cloud.aws.s3.bucket.url}")
private String defaultUrl;

public String uploadFile(MultipartFile multipartFile, String imageType) {
if (multipartFile == null || multipartFile.isEmpty()) return null;

String savedFileName = getSavedFileName(multipartFile, imageType);
ObjectMetadata metadata = new ObjectMetadata();

try (InputStream inputStream = multipartFile.getInputStream()) {
amazonS3Client.putObject(bucketName, savedFileName, inputStream, metadata);
} catch (IOException e) {
log.error("Failed to upload image", e);
throw new BusinessException(INVALID_FILE_UPLOAD);
}
return getResourceUrl(savedFileName);
}

public void deleteFile(String fileUrl) {
String fileName = getFileNameFromResourceUrl(fileUrl);
amazonS3Client.deleteObject(new DeleteObjectRequest(bucketName, fileName));
}

private String getSavedFileName(MultipartFile multipartFile, String imageType) {
return String.format("%s/%s-%s",
imageType, getRandomUUID(), multipartFile.getOriginalFilename());
}

private String getRandomUUID() {
return UUID.randomUUID().toString().replace("-", "");
}

private String getResourceUrl(String savedFileName) {
return amazonS3Client.getResourceUrl(bucketName, savedFileName);
}

private String getFileNameFromResourceUrl(String fileUrl) {
return fileUrl.replace(defaultUrl + "/", "");
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/project/mapdagu/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.project.mapdagu.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
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 S3Config {

@Value("${cloud.aws.credentials.access-key}")
private String accessKey;

@Value("${cloud.aws.credentials.secret-key}")
private String secretKey;

@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

return (AmazonS3Client) AmazonS3ClientBuilder
.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/project/mapdagu/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public enum ErrorCode {
FRIEND_REQUEST_NOT_FOUND(NOT_FOUND, "해당 친구 요청을 찾을 수 없습니다."),
FRIEND_NOT_FOUND(NOT_FOUND, "해당 회원과 친구가 아닙니다."),


INVALID_FILE_UPLOAD(BAD_REQUEST, "파일 업로드에 실패하였습니다."),
WRONG_SEARCH(BAD_REQUEST, "검색어를 입력해야 합니다."),
ALREADY_EXIST_EVALUATION(BAD_REQUEST, "이미 존재하는 평가입니다."),
ALREADY_LOGOUT_MEMBER(BAD_REQUEST, "이미 로그아웃한 회원입니다"),
Expand Down

0 comments on commit c7fc832

Please sign in to comment.