-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
138 additions
and
0 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
90 changes: 90 additions & 0 deletions
90
src/main/java/com/nextroom/nextRoomServer/util/aws/S3Component.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,90 @@ | ||
package com.nextroom.nextRoomServer.util.aws; | ||
|
||
import com.nextroom.nextRoomServer.exceptions.CustomException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.*; | ||
import software.amazon.awssdk.services.s3.presigner.S3Presigner; | ||
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; | ||
import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest; | ||
|
||
import java.time.Duration; | ||
import java.util.UUID; | ||
|
||
import static com.nextroom.nextRoomServer.exceptions.StatusCode.INTERNAL_SERVER_ERROR; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class S3Component { | ||
@Value("${spring.config.activate.on-profile}") | ||
private String profile; | ||
@Value("${spring.cloud.aws.s3.bucket}") | ||
private String bucketName; | ||
|
||
private final S3Client s3Client; | ||
private final S3Presigner presigner; | ||
|
||
private final static String EXTENSION = ".png"; | ||
|
||
public String createPresignedUrl(String fileName) { | ||
validateFileName(fileName); | ||
|
||
PutObjectRequest objectRequest = PutObjectRequest.builder() | ||
.bucket(bucketName) | ||
.key(fileName) | ||
.contentType("image/png") | ||
.build(); | ||
|
||
PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder() | ||
.signatureDuration(Duration.ofMinutes(10)) | ||
.putObjectRequest(objectRequest) | ||
.build(); | ||
|
||
return presigner.presignPutObject(presignRequest).url().toExternalForm(); | ||
} | ||
|
||
public String createPresignedGetUrl(String fileName) { | ||
validateFileName(fileName); | ||
|
||
GetObjectRequest objectRequest = GetObjectRequest.builder() | ||
.bucket(bucketName) | ||
.key(fileName) | ||
.build(); | ||
|
||
GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder() | ||
.signatureDuration(Duration.ofMinutes(10)) | ||
.getObjectRequest(objectRequest) | ||
.build(); | ||
|
||
return presigner.presignGetObject(presignRequest).url().toExternalForm(); | ||
} | ||
|
||
public void deleteObject(String fileName) { | ||
validateFileName(fileName); | ||
|
||
try { | ||
DeleteObjectRequest objectRequest = DeleteObjectRequest.builder() | ||
.bucket(bucketName) | ||
.key(fileName) | ||
.build(); | ||
|
||
s3Client.deleteObject(objectRequest); | ||
} catch (S3Exception e) { | ||
System.err.println(e.awsErrorDetails().errorMessage()); | ||
System.exit(1); | ||
throw new CustomException(INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
public String createFileName(Long shopId, Long themeId, String type, int i) { | ||
return String.format("%s/%s/%s/%s/%s_%s%s", profile, shopId, themeId, type, i, UUID.randomUUID(), EXTENSION); | ||
} | ||
|
||
private void validateFileName(String fileName) { | ||
if (fileName == null || fileName.isEmpty()) { | ||
throw new CustomException(INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/nextroom/nextRoomServer/util/aws/S3Config.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,43 @@ | ||
package com.nextroom.nextRoomServer.util.aws; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.presigner.S3Presigner; | ||
|
||
@Configuration | ||
public class S3Config { | ||
|
||
@Value("${spring.cloud.aws.s3.credentials.accessKey}") | ||
private String accessKey; | ||
|
||
@Value("${spring.cloud.aws.s3.credentials.secretKey}") | ||
private String secretKey; | ||
|
||
@Value("${spring.cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Bean | ||
public S3Client s3Client() { | ||
return S3Client.builder() | ||
.credentialsProvider(StaticCredentialsProvider.create(awsCredentials())) | ||
.region(Region.of(region)) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public S3Presigner presigner() { | ||
return S3Presigner.builder() | ||
.credentialsProvider(StaticCredentialsProvider.create(awsCredentials())) | ||
.region(Region.of(region)) | ||
.build(); | ||
} | ||
|
||
private AwsBasicCredentials awsCredentials() { | ||
return AwsBasicCredentials.create(accessKey, secretKey); | ||
} | ||
} |