Skip to content

Commit

Permalink
Merge pull request #38 from KUSITMS-29th-TEAM-D/feature/#35/share
Browse files Browse the repository at this point in the history
[feat] : S3 CORS 설정 추가
  • Loading branch information
bbbang105 authored May 17, 2024
2 parents 128b71a + 08f1344 commit 273da15
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
46 changes: 39 additions & 7 deletions src/main/java/kusitms/jangkku/global/config/NcpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.BucketCrossOriginConfiguration;
import com.amazonaws.services.s3.model.CORSRule;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class NcpConfig {

@Value("${cloud.aws.credentials.access-key}")
private String accessKey;

Expand All @@ -23,14 +29,40 @@ public class NcpConfig {
@Value("${cloud.aws.s3.endpoint}")
private String endPoint;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
@Value("${cloud.aws.credentials.bucket}")
private String bucketName;

return (AmazonS3Client) AmazonS3ClientBuilder
.standard()
@Bean
public AmazonS3 amazonS3() {
return AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, region))
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
}

@Bean
public BucketCrossOriginConfiguration bucketCrossOriginConfiguration(AmazonS3 amazonS3) {
configureS3Cors(amazonS3);
return new BucketCrossOriginConfiguration();
}

public void configureS3Cors(AmazonS3 s3) {
List<CORSRule.AllowedMethods> methodRule = new ArrayList<>();
methodRule.add(CORSRule.AllowedMethods.PUT);
methodRule.add(CORSRule.AllowedMethods.GET);
methodRule.add(CORSRule.AllowedMethods.POST);
CORSRule rule = new CORSRule().withId("CORSRule")
.withAllowedMethods(methodRule)
.withAllowedHeaders(List.of("*"))
.withAllowedOrigins(List.of("*"))
.withMaxAgeSeconds(3000);

List<CORSRule> rules = new ArrayList<>();
rules.add(rule);

BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();
configuration.setRules(rules);

s3.setBucketCrossOriginConfiguration(bucketName, configuration);
}
}
4 changes: 2 additions & 2 deletions src/main/java/kusitms/jangkku/global/util/S3Util.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kusitms.jangkku.global.util;

import com.amazonaws.services.s3.AmazonS3Client;
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;
Expand All @@ -21,7 +21,7 @@ public class S3Util {
@Value("${cloud.aws.s3.endpoint}")
private String END_POINT;

private final AmazonS3Client amazonS3Client;
private final AmazonS3 amazonS3Client;

// S3에 파일을 업로드하는 메서드
public String uploadProfileImg(MultipartFile file) {
Expand Down

0 comments on commit 273da15

Please sign in to comment.