-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/main/java/org/alongtheblue/alongtheblue_server/global/adapter/S3Adapter.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,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); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/alongtheblue/alongtheblue_server/global/config/S3Config.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,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(); | ||
} | ||
} |
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