Skip to content

Commit

Permalink
[Feat] S3 설정파일 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonInbae committed Sep 20, 2024
1 parent a3d6dd9 commit 38b9e86
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.alongtheblue.alongtheblue_server.global.adapter;

import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import lombok.RequiredArgsConstructor;
import org.alongtheblue.alongtheblue_server.global.common.response.ApiResponse;
import org.alongtheblue.alongtheblue_server.global.error.ErrorCode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

@RequiredArgsConstructor
@Service
public class S3Adapter {

private final AmazonS3Client amazonS3Client;

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


public ApiResponse<String> uploadImage(MultipartFile multipartFile) {
ObjectMetadata metadata = new ObjectMetadata();
String fileName = UUID.randomUUID() + ".png";
metadata.setContentLength(multipartFile.getSize());
metadata.setContentType("image/png");
try {
amazonS3Client.putObject(bucket, fileName, multipartFile.getInputStream(), metadata);
return ApiResponse.ok("S3 버킷에 이미지 업로드를 성공하였습니다.", amazonS3Client.getUrl(bucket, fileName).toString());
} catch (IOException e) {
return ApiResponse.withError(ErrorCode.ERROR_S3_UPDATE_OBJECT);
}
}

public ApiResponse<String> uploadFile(MultipartFile multipartFile) throws IOException {
String originalFilename = multipartFile.getOriginalFilename();

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(multipartFile.getSize());
metadata.setContentType(multipartFile.getContentType());

try {
amazonS3Client.putObject(bucket, originalFilename, multipartFile.getInputStream(), metadata);
return ApiResponse.ok("S3 버킷에 이미지 업로드를 성공하였습니다.", amazonS3Client.getUrl(bucket, originalFilename).toString());
}
catch (IOException e) {
return ApiResponse.withError(ErrorCode.ERROR_S3_UPDATE_OBJECT);
}
}



public ApiResponse<String> deleteFile(String fileName){
try{
amazonS3Client.deleteObject(bucket, fileName);
return ApiResponse.ok("S3 버킷에서 이미지를 성공적으로 삭제하였습니다.", amazonS3Client.getUrl(bucket, fileName).toString());

}catch (SdkClientException e){
return ApiResponse.withError(ErrorCode.ERROR_S3_DELETE_OBJECT);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.alongtheblue.alongtheblue_server.global.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("${aws.s3.access-key}")
private String accessKey;
@Value("${aws.s3.secret-key}")
private String secretKey;
@Value("${aws.s3.region}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client)AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
@RequiredArgsConstructor
public enum ErrorCode {
// Common
INVALID_INPUT_VALUE(HttpStatus.BAD_REQUEST, "Request Body를 통해 전달된 값이 유효하지 않습니다.");
INVALID_INPUT_VALUE(HttpStatus.BAD_REQUEST, "Request Body를 통해 전달된 값이 유효하지 않습니다."),

// S3
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 문제로 S3 이미지 업로드에 실패하였습니다."),
NOT_EXIST_IMAGE_FILE(HttpStatus.BAD_REQUEST, "업로드 할 이미지가 존재하지 않습니다."),

ERROR_S3_DELETE_OBJECT(HttpStatus.INTERNAL_SERVER_ERROR, "서버 문제 S3 이미지 삭제에 실패하였습니다."),
ERROR_S3_UPDATE_OBJECT(HttpStatus.INTERNAL_SERVER_ERROR, "서버 문제로 S3 이미지 업로드에 실패하였습니다.");

private final HttpStatus status;
private final String message;
Expand Down

0 comments on commit 38b9e86

Please sign in to comment.