Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
// s3
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.763'
// QueryDSL
implementation "com.querydsl:querydsl-apt:${queryDslVersion}"
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}:jakarta"
Expand All @@ -78,6 +80,8 @@ dependencies {
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.3.Final'

implementation "javax.annotation:javax.annotation-api:1.3.2"
implementation 'commons-io:commons-io:2.16.1'

}

def generatedFilesBaseDir = "${projectDir}/src/generated"
Expand Down
1 change: 1 addition & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.example.yesable_be.component.aws;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
@RequiredArgsConstructor
@Component
public class S3UploadComponent {

@Qualifier("amazonS3Builder")
private final AmazonS3 amazonS3;

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

/**
* s3 버킷에 올라간 파일 삭제
*/
public void deleteFile(String filePath) {
boolean isExistObject = amazonS3.doesObjectExist(yesableBucket, filePath);
if (isExistObject) {
amazonS3.deleteObject(yesableBucket, filePath);
}
}

/**
* s3 버킷에 파일 업로드
*/
public void uploadFile(InputStream inputStream, ObjectMetadata objectMetadata, String fileName) throws IOException {
byte[] bytes = IOUtils.toByteArray(inputStream);
objectMetadata.setContentLength(bytes.length);
objectMetadata.setCacheControl("max-age=10");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

PutObjectRequest request = new PutObjectRequest(yesableBucket, fileName, byteArrayInputStream, objectMetadata).withCannedAcl(CannedAccessControlList.Private);
try {
amazonS3.putObject(request);
} catch (Exception e) {
// TODO : Error Handling
}
}

/**
* S3 버킷 파일 경로 획득
*/
public String getFileUrl(String fileName) {
return amazonS3.getUrl(yesableBucket, fileName).toString();
}


}
36 changes: 36 additions & 0 deletions src/main/java/com/example/yesable_be/config/aws/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.yesable_be.config.aws;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
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 s3AccessKey;
@Value("${cloud.aws.credentials.secret-key}")
private String s3SecretKey;
@Value("${cloud.aws.region.static}")
private String region;

@Bean(name = "amazonS3Builder")
public AmazonS3 amazonS3() {

return AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(buildAWSStaticCredentialsProvider(s3AccessKey, s3SecretKey))
.build();
}

private AWSStaticCredentialsProvider buildAWSStaticCredentialsProvider(String accessKey, String secretKey) {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
return new AWSStaticCredentialsProvider(awsCredentials);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.yesable_be.constant;

public class CommonConstants {
public static final String PNG = "png";
public static final String PNG_CONTENT_TYPE = "image/png";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.yesable_be.service.s3;

import com.amazonaws.services.s3.model.ObjectMetadata;
import com.example.yesable_be.component.aws.S3UploadComponent;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

import static com.example.yesable_be.constant.CommonConstants.*;

@Component
@RequiredArgsConstructor
public class UploadExecutor {
private final S3UploadComponent s3UploadComponent;
private final Environment env;

/**
* png 파일 업로드
*/
public String pngFileUpload(InputStream file, String filePath) throws IOException {
//TODO : exception handling 처리 후 try-catch 로 변경
ObjectMetadata metaData = new ObjectMetadata();
metaData.setContentType(PNG_CONTENT_TYPE);
metaData.setContentLength(file.available());
metaData.setCacheControl("max-age=600");

s3UploadComponent.uploadFile(file, metaData, filePath);
return "https://" + env.getProperty("aws.s3.bucket") + "/" + filePath;
}

}