-
Notifications
You must be signed in to change notification settings - Fork 1
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
6 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
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
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
36 changes: 36 additions & 0 deletions
36
src/main/java/HeyPorori/transaction/config/AmazonS3Config.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,36 @@ | ||
package HeyPorori.transaction.config; | ||
|
||
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; | ||
import org.springframework.context.annotation.Primary; | ||
|
||
@Configuration | ||
public class AmazonS3Config { | ||
@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 | ||
@Primary | ||
public BasicAWSCredentials awsCredentialsProvider(){ | ||
return new BasicAWSCredentials(accessKey, secretKey); | ||
} | ||
|
||
@Bean | ||
public AmazonS3 amazonS3Client() { | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentialsProvider())) | ||
.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
14 changes: 14 additions & 0 deletions
14
src/main/java/HeyPorori/transaction/dto/PreSignedUrlRes.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,14 @@ | ||
package HeyPorori.transaction.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class PreSignedUrlRes { | ||
private String url; | ||
|
||
@Builder | ||
public PreSignedUrlRes(String url) { | ||
this.url = url; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/HeyPorori/transaction/service/AmazonS3Service.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,55 @@ | ||
package HeyPorori.transaction.service; | ||
|
||
import HeyPorori.transaction.dto.PreSignedUrlRes; | ||
import com.amazonaws.HttpMethod; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.Headers; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.transaction.Transactional; | ||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class AmazonS3Service { | ||
private final AmazonS3 amazonS3; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
public PreSignedUrlRes getPreSignedUrl(){ | ||
String uuid = UUID.randomUUID().toString(); | ||
String objectKey = "transactions/"+uuid; | ||
|
||
GeneratePresignedUrlRequest request = generatePresignedUrlRequest(bucket, objectKey); | ||
PreSignedUrlRes response = PreSignedUrlRes.builder() | ||
.url(amazonS3.generatePresignedUrl(request).toString()) | ||
.build(); | ||
return response; | ||
} | ||
|
||
private GeneratePresignedUrlRequest generatePresignedUrlRequest(String bucket, String imageName){ | ||
Date expiration = new Date(); | ||
long expTimeMillis = expiration.getTime(); | ||
expTimeMillis += 1000 * 60 * 5; // 5분 | ||
expiration.setTime(expTimeMillis); | ||
|
||
//Pre-Signed Url request 생성 | ||
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, imageName) | ||
.withMethod(HttpMethod.PUT) | ||
.withExpiration(expiration); | ||
|
||
//request 파라미터 추가 | ||
request.addRequestParameter( | ||
Headers.S3_CANNED_ACL, | ||
CannedAccessControlList.PublicRead.toString()); | ||
|
||
return request; | ||
} | ||
} |