Skip to content

Commit

Permalink
feat: #12 S3 이미지 업로드 유틸 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
youngeun-dev committed Feb 29, 2024
1 parent 6085410 commit 67b53d1
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
}

tasks.named('test') {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/server/capple/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.server.capple.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();
}
}
59 changes: 59 additions & 0 deletions src/main/java/com/server/capple/global/utils/S3ImageComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.server.capple.global.utils;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

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

@Component
@RequiredArgsConstructor
public class S3ImageComponent {
private final AmazonS3 amazonS3;

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

/**
* 이미지 업로드
* @param multipartFile 업로드 할 이미지 파일
* @return 업로드된 파일의 접근 URL
*/
public String uploadImage(MultipartFile multipartFile) throws IOException {
String fileName = createFileName(multipartFile.getOriginalFilename());

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

amazonS3.putObject(bucket, fileName, multipartFile.getInputStream(), metadata);
return amazonS3.getUrl(bucket, fileName).toString();
}

/**
* 이미지 파일명 생성
* @param originalFileName 파일의 이름
* @return 작명된 파일 이름
*/
public String createFileName(String originalFileName) {
return getOriginalFileName(originalFileName) + UUID.randomUUID().toString().replaceAll("-","") + getFileExtension(originalFileName);
}

private String getFileExtension(String fileName) {
return fileName.substring(fileName.lastIndexOf("."));
}
private String getOriginalFileName(String fileName) { return fileName.substring(0, fileName.lastIndexOf(".")); }

/**
* 버킷에서 이미지 삭제
* @param fileUrl
*/
public void deleteImage(String fileUrl) {
amazonS3.deleteObject(new DeleteObjectRequest(bucket, fileUrl.split("/", 4)[3]));
}
}

0 comments on commit 67b53d1

Please sign in to comment.