Search in sources :

Example 1 with ListObjectVersionsResponse

use of software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse in project formkiq-core by formkiq.

the class DocumentVersionsRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    SimpleDateFormat df = new SimpleDateFormat(GsonUtil.DATE_FORMAT);
    String siteId = authorizer.getSiteId();
    String documentId = event.getPathParameters().get("documentId");
    String next = getParameter(event, "next");
    String tz = getParameter(event, "tz");
    ZoneOffset offset = DateUtil.getZoneOffset(tz);
    df.setTimeZone(TimeZone.getTimeZone(offset));
    String s3key = createDatabaseKey(siteId, documentId);
    S3Service s3service = awsservice.s3Service();
    try (S3Client s3 = s3service.buildClient()) {
        ListObjectVersionsResponse response = s3service.getObjectVersions(s3, awsservice.documents3bucket(), s3key, next);
        List<ApiDocumentVersion> list = response.versions().stream().map(v -> {
            ApiDocumentVersion dv = new ApiDocumentVersion();
            dv.setVersionId(v.versionId());
            Date date = Date.from(v.lastModified());
            dv.setLastModifiedDate(df.format(date));
            return dv;
        }).collect(Collectors.toList());
        ApiDocumentVersionsResponse resp = new ApiDocumentVersionsResponse();
        resp.setNext(response.nextKeyMarker());
        resp.setVersions(list);
        return new ApiRequestHandlerResponse(SC_OK, resp);
    }
}
Also used : Date(java.util.Date) TimeZone(java.util.TimeZone) S3Client(software.amazon.awssdk.services.s3.S3Client) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) SimpleDateFormat(java.text.SimpleDateFormat) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) Collectors(java.util.stream.Collectors) SiteIdKeyGenerator.createDatabaseKey(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.createDatabaseKey) List(java.util.List) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) GsonUtil(com.formkiq.lambda.apigateway.util.GsonUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) DateUtil(com.formkiq.stacks.dynamodb.DateUtil) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) ApiDocumentVersionsResponse(com.formkiq.stacks.api.ApiDocumentVersionsResponse) S3Service(com.formkiq.aws.s3.S3Service) ApiDocumentVersion(com.formkiq.stacks.api.ApiDocumentVersion) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse) ApiDocumentVersionsResponse(com.formkiq.stacks.api.ApiDocumentVersionsResponse) ApiDocumentVersion(com.formkiq.stacks.api.ApiDocumentVersion) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) SimpleDateFormat(java.text.SimpleDateFormat) S3Service(com.formkiq.aws.s3.S3Service) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse) Date(java.util.Date) ZoneOffset(java.time.ZoneOffset)

Example 2 with ListObjectVersionsResponse

use of software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse in project aws-sdk-java-v2 by aws.

the class S3IntegrationTestBase method deleteBucketAndAllContents.

protected static void deleteBucketAndAllContents(String bucketName) {
    System.out.println("Deleting S3 bucket: " + bucketName);
    ListObjectsResponse response = s3.listObjects(ListObjectsRequest.builder().bucket(bucketName).build());
    while (true) {
        if (response.contents() == null) {
            break;
        }
        for (S3Object objectSummary : response.contents()) {
            s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(objectSummary.key()).build());
        }
        if (response.isTruncated()) {
            response = s3.listObjects(ListObjectsRequest.builder().marker(response.nextMarker()).build());
        } else {
            break;
        }
    }
    ListObjectVersionsResponse versionsResponse = s3.listObjectVersions(ListObjectVersionsRequest.builder().bucket(bucketName).build());
    if (versionsResponse.versions() != null) {
        for (ObjectVersion s : versionsResponse.versions()) {
            s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(s.key()).versionId(s.versionId()).build());
        }
    }
    s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
}
Also used : ObjectVersion(software.amazon.awssdk.services.s3.model.ObjectVersion) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse)

Example 3 with ListObjectVersionsResponse

use of software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse in project aws-sdk-java-v2 by aws.

the class CloudTrailIntegrationTest method deleteBucketAndAllContents.

public static void deleteBucketAndAllContents(String bucketName) {
    System.out.println("Deleting S3 bucket: " + bucketName);
    ListObjectsResponse response = s3.listObjects(ListObjectsRequest.builder().bucket(bucketName).build());
    while (true) {
        if (response.contents() == null) {
            break;
        }
        for (Iterator<?> iterator = response.contents().iterator(); iterator.hasNext(); ) {
            S3Object objectSummary = (S3Object) iterator.next();
            s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(objectSummary.key()).build());
        }
        if (response.isTruncated()) {
            response = s3.listObjects(ListObjectsRequest.builder().marker(response.nextMarker()).build());
        } else {
            break;
        }
    }
    ListObjectVersionsResponse versionsResponse = s3.listObjectVersions(ListObjectVersionsRequest.builder().bucket(bucketName).build());
    if (versionsResponse.versions() != null) {
        for (ObjectVersion s : versionsResponse.versions()) {
            s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(s.key()).versionId(s.versionId()).build());
        }
    }
    s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
}
Also used : ObjectVersion(software.amazon.awssdk.services.s3.model.ObjectVersion) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse)

Example 4 with ListObjectVersionsResponse

use of software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse in project aws-sdk-java-v2 by aws.

the class DecodeUrlEncodedResponseInterceptorTest method encodingTypeSet_decodesListObjectVersionsResponse.

@Test
public void encodingTypeSet_decodesListObjectVersionsResponse() {
    Context.ModifyResponse ctx = newContext(TEST_LIST_OBJECT_VERSION_RESPONSE);
    ListObjectVersionsResponse decoded = (ListObjectVersionsResponse) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes());
    assertDecoded(decoded::delimiter, " delimiter");
    assertDecoded(decoded::prefix, " prefix");
    assertDecoded(decoded::keyMarker, " keyMarker");
    assertDecoded(decoded::nextKeyMarker, " nextKeyMarker");
    assertCommonPrefixesAreDecoded(decoded.commonPrefixes());
    assertVersionsAreDecoded(decoded.versions());
}
Also used : Context(software.amazon.awssdk.core.interceptor.Context) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse) Test(org.junit.jupiter.api.Test)

Example 5 with ListObjectVersionsResponse

use of software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse in project aws-sdk-java-v2 by aws.

the class UrlEncodingIntegrationTest method listObjectWithUrlEncodingType_shouldDecode.

@Test
public void listObjectWithUrlEncodingType_shouldDecode() {
    ListObjectsResponse listObjectsV2Response = s3.listObjects(b -> b.bucket(BUCKET_NAME).encodingType(EncodingType.URL));
    listObjectsV2Response.contents().forEach(c -> assertKeyIsDecoded(c.key()));
    ListObjectVersionsResponse asyncResponse = s3Async.listObjectVersions(b -> b.bucket(BUCKET_NAME).encodingType(EncodingType.URL)).join();
    asyncResponse.versions().forEach(v -> assertKeyIsDecoded(v.key()));
}
Also used : AfterClass(org.junit.AfterClass) ListMultipartUploadsResponse(software.amazon.awssdk.services.s3.model.ListMultipartUploadsResponse) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) S3BucketUtils.temporaryBucketName(software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName) Test(org.junit.Test) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response) EncodingType(software.amazon.awssdk.services.s3.model.EncodingType) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) RequestBody(software.amazon.awssdk.core.sync.RequestBody) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse) UploadPartResponse(software.amazon.awssdk.services.s3.model.UploadPartResponse) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse) Test(org.junit.Test)

Aggregations

ListObjectVersionsResponse (software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse)10 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 BeforeClass (org.junit.BeforeClass)4 RequestBody (software.amazon.awssdk.core.sync.RequestBody)4 S3Object (software.amazon.awssdk.services.s3.model.S3Object)4 List (java.util.List)3 RandomStringUtils (org.apache.commons.lang3.RandomStringUtils)3 AfterClass (org.junit.AfterClass)3 Test (org.junit.Test)3 S3Client (software.amazon.awssdk.services.s3.S3Client)3 CreateMultipartUploadResponse (software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse)3 EncodingType (software.amazon.awssdk.services.s3.model.EncodingType)3 ListMultipartUploadsResponse (software.amazon.awssdk.services.s3.model.ListMultipartUploadsResponse)3 ListObjectsV2Response (software.amazon.awssdk.services.s3.model.ListObjectsV2Response)3 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)3 UploadPartResponse (software.amazon.awssdk.services.s3.model.UploadPartResponse)3 S3BucketUtils.temporaryBucketName (software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName)3 Iterator (java.util.Iterator)2 Context (software.amazon.awssdk.core.interceptor.Context)2