Skip to content

Commit

Permalink
✨ feat: S3 저장 로직 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
woosung1223 committed Sep 30, 2024
1 parent cc9490f commit dbabf45
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ dependencies {

// Spring Security 설정
implementation 'org.springframework.boot:spring-boot-starter-security'

implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
}

tasks.register('importConfigSubmodule', Copy) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package slvtwn.khu.toyouserver.agent.aws;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Getter
@Configuration
public class AwsConfiguration {

@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;
@Value("${cloud.aws.s3.bucket}")
private String bucket;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}
}
35 changes: 35 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/agent/aws/S3Agent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package slvtwn.khu.toyouserver.agent.aws;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import java.io.ByteArrayInputStream;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import slvtwn.khu.toyouserver.common.response.ResponseType;
import slvtwn.khu.toyouserver.exception.ToyouException;

@RequiredArgsConstructor
@Component
public class S3Agent {

private final AmazonS3Client amazonS3Client;
private final AwsConfiguration configuration;

public String uploadFile(byte[] fileData, String fileName, String contentType) {
try {
String fileUrl = "https://" + configuration.getBucket() + "/to-you/" + fileName;

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(contentType);
metadata.setContentLength(fileData.length);

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileData);
amazonS3Client.putObject(new PutObjectRequest(configuration.getBucket(), fileName, byteArrayInputStream, metadata));

return fileUrl;
} catch (Exception e) {
throw new ToyouException(ResponseType.INTERNAL_SERVER_ERROR);
}
}
}

0 comments on commit dbabf45

Please sign in to comment.