Skip to content

Commit

Permalink
added getPresigned url
Browse files Browse the repository at this point in the history
  • Loading branch information
candreac committed Oct 31, 2023
1 parent afe8a84 commit a13b19b
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;

import java.io.File;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -76,6 +77,13 @@ public RestMulti<Buffer> downloadFile(String objectKey) {
List.of(response.response().contentType())));
}

@GET
@Path("presigned-url/{objectKey}")
@Produces(MediaType.APPLICATION_JSON)
public Uni<URL> getPresigned(String objectKey) {
return bpmnFileStorageService.generatePresignedUrl(objectKey);
}

@GET
public Uni<List<FileObject>> listFiles() {
ListObjectsRequest listRequest = ListObjectsRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import software.amazon.awssdk.services.s3.model.PutObjectResponse;

import java.io.File;
import java.net.URL;

public interface BpmnFileStorageService {
Uni<PutObjectResponse> uploadFile(BpmnIdDto bpmnVersionPK, File file, String filename);
Uni<URL> generatePresignedUrl(String objectKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import software.amazon.awssdk.services.s3.model.PutObjectResponse;

import java.io.File;
import java.net.URL;

public interface ObjectStoreService {


ObjectStoreStrategyEnum getType();

Uni<PutObjectResponse> uploadFile(File file, String path, ResourceTypeEnum fileType, String filename);

Uni<URL> generatePresignedUrl(String objectKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import software.amazon.awssdk.services.s3.model.PutObjectResponse;

import java.io.File;
import java.net.URL;

public interface S3ObjectStoreService extends ObjectStoreService {
Uni<PutObjectResponse> uploadFile(File file, String path, ResourceTypeEnum fileType, String filename);

Uni<URL> generatePresignedUrl(String objectKey);

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package it.gov.pagopa.atmlayer.service.model.service.impl;

import io.smallrye.mutiny.Uni;
import it.gov.pagopa.atmlayer.service.model.entity.BpmnVersionPK;
import it.gov.pagopa.atmlayer.service.model.enumeration.ObjectStoreStrategyEnum;
import it.gov.pagopa.atmlayer.service.model.enumeration.ResourceTypeEnum;
import it.gov.pagopa.atmlayer.service.model.model.BpmnIdDto;
Expand All @@ -17,6 +16,7 @@
import software.amazon.awssdk.services.s3.model.PutObjectResponse;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -49,6 +49,11 @@ public Uni<PutObjectResponse> uploadFile(BpmnIdDto bpmnVersionPK, File file, Str

}

@Override
public Uni<URL> generatePresignedUrl(String objectKey) {
return this.objectStoreService.generatePresignedUrl(objectKey);
}

private String calculatePath(BpmnIdDto bpmnVersionPK) {
Map<String, String> valuesMap = new HashMap<>();
valuesMap.put("uuid", bpmnVersionPK.getBpmnId().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
import org.apache.commons.lang3.StringUtils;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
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.PresignedGetObjectRequest;

import java.io.File;
import java.net.URL;
import java.time.Duration;
import java.util.Objects;

@ApplicationScoped
Expand All @@ -30,11 +36,34 @@ public class S3ObjectStoreServiceImpl implements S3ObjectStoreService {
@Inject
FileStorageS3Utils fileStorageS3Utils;

@Inject
S3Presigner presigner;

@Override
public ObjectStoreStrategyEnum getType() {
return ObjectStoreStrategyEnum.AWS_S3;
}


@Override
public Uni<URL> generatePresignedUrl(String objectKey) {
try (S3Presigner presigner = S3Presigner.create()) {
GetObjectRequest getObjectRequest = fileStorageS3Utils.buildGetRequest(objectKey);

// Genera un URL prefirmato per l'oggetto
GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(10)) // The URL will expire in 10 minutes.
.getObjectRequest(getObjectRequest)
.build();

PresignedGetObjectRequest presignedRequest = presigner.presignGetObject(getObjectPresignRequest);
String myURL = presignedRequest.url().toString();
log.info("Presigned URL to upload a file to: [{}]", myURL);

return Uni.createFrom().item(presignedRequest.url());
}
}

public Uni<PutObjectResponse> uploadFile(File file, String path, ResourceTypeEnum fileType, String filename) {
if (StringUtils.isBlank(filename)) {
String errorMessage = String.format("S3 File Upload - invalid filename %s", filename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ObjectStoreStrategy {
public ObjectStoreService getType(ObjectStoreStrategyEnum objectStoreStrategyEnum) {
ObjectStoreService objectStoreService = selectObjectStoreByType.getOrDefault(objectStoreStrategyEnum, null);
if (Objects.isNull(objectStoreService)) {
throw new AtmLayerException(String.format("Object Store Service not Found: %s", objectStoreStrategyEnum.name()), Response.Status.INTERNAL_SERVER_ERROR, "INTERNAL");
throw new AtmLayerException(String.format("Object Store Service not Found : %s", objectStoreStrategyEnum.name()), Response.Status.INTERNAL_SERVER_ERROR, "INTERNAL");
}
log.info("objectStoreService: {}", objectStoreService);
return objectStoreService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.gov.pagopa.atmlayer.service.model.properties.ObjectStoreProperties;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;

@ApplicationScoped
Expand All @@ -18,4 +19,11 @@ public PutObjectRequest buildPutRequest(String filename, String mimetype, String
.build();
}

public GetObjectRequest buildGetRequest(String key) {
return GetObjectRequest.builder()
.bucket(objectStoreProperties.bucket().name())
.key(key)
.build();
}

}
2 changes: 1 addition & 1 deletion src/main/resources/application-local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ quarkus.s3.endpoint-override=http://127.0.0.1:9999
quarkus.s3.aws.credentials.type=static
quarkus.s3.aws.credentials.static-provider.access-key-id=${OBJECT_STORE_ACCESS_KEY_ID:admin123}
quarkus.s3.aws.credentials.static-provider.secret-access-key=${OBJECT_STORE_ACCESS_KEY_SECRET:admin123}
quarkus.s3.aws.region=${MODEL_OBJECT_STORE_REGION:eu-west-1}
quarkus.s3.aws.region=${MODEL_OBJECT_STORE_REGION:eu-south-1}
quarkus.devservices.enabled=false
quarkus.log.category."software.amazon.awssdk.services.s3".level=DEBUG
###################
Expand Down

0 comments on commit a13b19b

Please sign in to comment.