Skip to content

Commit

Permalink
Try fix missing credentials when gettin presinged url
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-sexenian committed Nov 23, 2023
1 parent 19f5ed7 commit 68a3ac5
Showing 1 changed file with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.net.URI;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.*;


Expand Down Expand Up @@ -68,6 +69,7 @@ public class ExternalProviderS3 extends ExternalProviderBase implements External
static final String DUALSTACK = "s3-accelerate.dualstack.amazonaws.com";
static final String DEFAULT_REGION = "us-east-1";
private S3Client client;
private S3Presigner presigner;
private String clientRegion = "";
private String bucket;
private String folder;
Expand All @@ -93,7 +95,7 @@ public ExternalProviderS3(GXService providerService) throws Exception{
initialize();
}

private void initialize() throws Exception{
private void initialize() throws Exception {
String accessKey = getEncryptedPropertyValue(ACCESS_KEY, ACCESS_KEY_ID_DEPRECATED, "");
String secretKey = getEncryptedPropertyValue(SECRET_ACCESS_KEY, SECRET_ACCESS_KEY_DEPRECATED, "");
String bucket = getEncryptedPropertyValue(BUCKET, BUCKET_DEPRECATED);
Expand All @@ -119,6 +121,7 @@ private void initialize() throws Exception{
this.folder = folder;

this.client = buildS3Client(accessKey, secretKey, endpointValue, clientRegion);
this.presigner = buildS3Presinger(accessKey, secretKey, clientRegion);
bucketExists();
}
}
Expand Down Expand Up @@ -179,6 +182,13 @@ else if (endpoint.equals(DUALSTACK)) {
return s3Client;
}

private S3Presigner buildS3Presinger(String accessKey, String secretKey, String region){
return S3Presigner.builder()
.region(Region.of(region))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
.build();
}

private void bucketExists() {
// There is no "bucket.exists" method, so we attempt to get metadata about the bucket
// and if we get a 404 error then it means the bucket does not exist
Expand Down Expand Up @@ -303,34 +313,24 @@ public String get(String externalFileName, ResourceAccessControlList acl, int ex
return getResourceUrl(externalFileName, acl, expirationMinutes);
}

private String getResourceUrl(String externalFileName, ResourceAccessControlList acl, int expirationMinutes) {
public String getResourceUrl(String externalFileName, ResourceAccessControlList acl, int expirationMinutes) {
if (internalToAWSACL(acl) == ObjectCannedACL.PRIVATE) {
final int finalExpirationMinutes = expirationMinutes > 0 ? expirationMinutes: defaultExpirationMinutes;
Date expiration = new Date();
long msec = expiration.getTime();
msec += 60000 * expirationMinutes;
expiration.setTime(msec);

S3Presigner presigner = S3Presigner.builder()
.region(Region.of(clientRegion))
.build();
expirationMinutes = expirationMinutes > 0 ? expirationMinutes : defaultExpirationMinutes;
Instant expiration = Instant.now().plus(Duration.ofMinutes(expirationMinutes));

GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(bucket)
.key(externalFileName)
.build();

PresignedGetObjectRequest presignedRequest = presigner.presignGetObject(z -> z.signatureDuration(Duration.ofMinutes(finalExpirationMinutes))
.getObjectRequest(getObjectRequest));

String presignedUrl = presignedRequest.url().toString();
presigner.close();
PresignedGetObjectRequest presignedGetObjectRequest =
presigner.presignGetObject(r -> r.signatureDuration(Duration.between(Instant.now(), expiration))
.getObjectRequest(getObjectRequest));

return presignedUrl;
return presignedGetObjectRequest.url().toString();
} else {
return String.format("https://%s.s3.%s.amazonaws.com/%s", bucket, Region.of(clientRegion), externalFileName);
}

}

public void delete(String objectName, ResourceAccessControlList acl) {
Expand Down

0 comments on commit 68a3ac5

Please sign in to comment.