@Override public VersionListing listNextBatchOfVersions(ListNextBatchOfVersionsRequest listNextBatchOfVersionsRequest) { listNextBatchOfVersionsRequest = beforeClientExecution(listNextBatchOfVersionsRequest); rejectNull(listNextBatchOfVersionsRequest, "The request object parameter must be specified when listing the next batch of versions in a bucket"); VersionListing previousVersionListing = listNextBatchOfVersionsRequest.getPreviousVersionListing(); if (!previousVersionListing.isTruncated()) { VersionListing emptyListing = new VersionListing(); emptyListing.setBucketName(previousVersionListing.getBucketName()); emptyListing.setDelimiter(previousVersionListing.getDelimiter()); emptyListing.setKeyMarker(previousVersionListing.getNextKeyMarker()); emptyListing.setVersionIdMarker(previousVersionListing.getNextVersionIdMarker()); emptyListing.setMaxKeys(previousVersionListing.getMaxKeys()); emptyListing.setPrefix(previousVersionListing.getPrefix()); emptyListing.setEncodingType(previousVersionListing.getEncodingType()); emptyListing.setTruncated(false); return emptyListing; } return listVersions(listNextBatchOfVersionsRequest.toListVersionsRequest()); }
@Override public VersionListing listVersions(ListVersionsRequest listVersionsRequest) throws SdkClientException, AmazonServiceException { listVersionsRequest = beforeClientExecution(listVersionsRequest); rejectNull(listVersionsRequest.getBucketName(), "The bucket name parameter must be specified when listing versions in a bucket"); /** * This flag shows whether we need to url decode S3 key names. This flag is enabled * only when the customers don't explicitly call {@link listVersionsRequest#setEncodingType(String)}, * otherwise, it will be disabled for maintaining backwards compatibility. */ final boolean shouldSDKDecodeResponse = listVersionsRequest.getEncodingType() == null; Request<ListVersionsRequest> request = createRequest(listVersionsRequest.getBucketName(), null, listVersionsRequest, HttpMethodName.GET); request.addParameter("versions", null); addParameterIfNotNull(request, "prefix", listVersionsRequest.getPrefix()); addParameterIfNotNull(request, "key-marker", listVersionsRequest.getKeyMarker()); addParameterIfNotNull(request, "version-id-marker", listVersionsRequest.getVersionIdMarker()); addParameterIfNotNull(request, "delimiter", listVersionsRequest.getDelimiter()); if (listVersionsRequest.getMaxResults() != null && listVersionsRequest.getMaxResults() >= 0) request.addParameter("max-keys", listVersionsRequest.getMaxResults().toString()); request.addParameter("encoding-type", shouldSDKDecodeResponse ? Constants.URL_ENCODING : listVersionsRequest.getEncodingType()); return invoke(request, new Unmarshallers.VersionListUnmarshaller(shouldSDKDecodeResponse), listVersionsRequest.getBucketName(), null); }
/** * Empties the contents of the given bucket and optionally deletes the bucket * * @param s3 The S3 client object to use * @param bucketName The bucket to empty * @param deleteBucket Flag to determine whether to delete the bucket after emptying * @param context Lambda context object */ public static void emptyBucket(AmazonS3 s3, String bucketName, boolean deleteBucket, Context context) { if (s3.doesBucketExistV2(bucketName)) { logDebug("Emptying bucket '" + bucketName + "'...", context); VersionListing versionListing = s3.listVersions(new ListVersionsRequest().withBucketName(bucketName)); while (true) { for (S3VersionSummary vs : versionListing.getVersionSummaries()) { s3.deleteVersion(bucketName, vs.getKey(), vs.getVersionId()); } if (versionListing.isTruncated()) { logDebug("Fetching next batch of versions for bucket '" + bucketName + "'", context); versionListing = s3.listNextBatchOfVersions(versionListing); } else { break; } } logInfo("Successfully emptied bucket '" + bucketName + "'", context); if (deleteBucket) { logDebug("Deleting bucket '" + bucketName + "'...", context); s3.deleteBucket(bucketName); logInfo("Successfully deleted bucket '" + bucketName + "'", context); } } else { logWarn("Bucket '" + bucketName + "' does not exist", context); } }
@Override public VersionListing listVersions(String bucketName, String prefix, String keyMarker, String versionIdMarker, String delimiter, Integer maxKeys) throws SdkClientException, AmazonServiceException { ListVersionsRequest request = new ListVersionsRequest() .withBucketName(bucketName) .withPrefix(prefix) .withDelimiter(delimiter) .withKeyMarker(keyMarker) .withVersionIdMarker(versionIdMarker) .withMaxResults(maxKeys); return listVersions(request); }
void deleteAngularjsApp(String websiteBucket, LambdaLogger logger) { logger.log("Removing AngularjsApp content from website versioned S3 bucket"); // We need to delete every version of every key ListVersionsRequest listVersionsRequest = new ListVersionsRequest() .withBucketName(websiteBucket); VersionListing versionListing; AmazonS3 client = TransferManagerBuilder.defaultTransferManager().getAmazonS3Client(); do { versionListing = client.listVersions(listVersionsRequest); versionListing .getVersionSummaries() .stream() .filter(k -> (k.getKey().startsWith("app"))) .forEach( k -> { logger.log("About to delete version: " + k.getVersionId() + " of AngularjsApp page: " + k.getKey()); DeleteVersionRequest deleteVersionRequest = new DeleteVersionRequest(websiteBucket, k.getKey(), k.getVersionId()); client.deleteVersion(deleteVersionRequest); logger.log("Successfully deleted version: " + k.getVersionId() + " of AngularjsApp page: " + k.getKey()); }); listVersionsRequest.setKeyMarker(versionListing.getNextKeyMarker()); } while (versionListing.isTruncated()); logger.log("Finished removing AngularjsApp content from website S3 bucket"); }
void removeSdkFromS3(LambdaLogger logger) { logger.log("About to remove apigateway sdk from website versioned S3 bucket"); // We need to delete every version of every key ListVersionsRequest listVersionsRequest = new ListVersionsRequest() .withBucketName(squashWebsiteBucket); VersionListing versionListing; IS3TransferManager transferManager = getS3TransferManager(); AmazonS3 client = transferManager.getAmazonS3Client(); do { versionListing = client.listVersions(listVersionsRequest); versionListing .getVersionSummaries() .stream() .filter( k -> !(k.getKey().startsWith("20") || k.getKey().equals("today.html") || k.getKey() .equals("bookings.html"))) .forEach( k -> { logger.log("About to delete version: " + k.getVersionId() + " of API SDK: " + k.getKey()); DeleteVersionRequest deleteVersionRequest = new DeleteVersionRequest( squashWebsiteBucket, k.getKey(), k.getVersionId()); client.deleteVersion(deleteVersionRequest); logger.log("Successfully deleted version: " + k.getVersionId() + " of API SDK key: " + k.getKey()); }); listVersionsRequest.setKeyMarker(versionListing.getNextKeyMarker()); } while (versionListing.isTruncated()); logger.log("Finished remove apigateway sdk from website S3 bucket"); }
@Test public void testDeleteDirectoryAssertHandleAmazonClientException() { S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations"); S3Operations mockS3Operations = mock(S3Operations.class); ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations); try { S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto(); s3FileTransferRequestParamsDto.setS3BucketName("s3BucketName"); s3FileTransferRequestParamsDto.setS3KeyPrefix("s3KeyPrefix"); VersionListing versionListing = new VersionListing(); versionListing.getVersionSummaries().add(new S3VersionSummary()); when(mockS3Operations.listVersions(any(), any())).thenReturn(versionListing); when(mockS3Operations.deleteObjects(any(), any())).thenThrow(new AmazonClientException("message")); try { s3Dao.deleteDirectory(s3FileTransferRequestParamsDto); fail(); } catch (Exception e) { assertEquals(IllegalStateException.class, e.getClass()); assertEquals("Failed to delete keys/key versions with prefix \"s3KeyPrefix\" from bucket \"s3BucketName\". Reason: message", e.getMessage()); } } finally { ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations); } }
@Test public void testDeleteDirectoryNoS3VersionsExist() { // Create an S3 file transfer request parameters DTO to access S3 objects. S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto(); s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME); s3FileTransferRequestParamsDto.setS3KeyPrefix(S3_KEY_PREFIX); // Create a retry policy. RetryPolicy retryPolicy = new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true); // Create an empty version listing. VersionListing versionListing = new VersionListing(); // Mock the external calls. when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy); when(s3Operations.listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class))).thenReturn(versionListing); // Call the method under test. s3DaoImpl.deleteDirectory(s3FileTransferRequestParamsDto); // Verify the external calls. verify(retryPolicyFactory).getRetryPolicy(); verify(s3Operations).listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class)); verifyNoMoreInteractionsHelper(); }
private void deleteAllVersions(String bucketName) { logger.info("Deleting all object versions from bucket '{}'", bucketName); VersionListing versionListing = s3.listVersions(bucketName, null); delete(versionListing); while (versionListing.isTruncated()) { versionListing = s3.listNextBatchOfVersions(versionListing); delete(versionListing); } }
@Override public VersionListing listVersions(String bucketName, String prefix) throws AmazonClientException, AmazonServiceException { return delegate.listVersions(bucketName, prefix); }
@Override public VersionListing listNextBatchOfVersions(VersionListing previousVersionListing) throws AmazonClientException, AmazonServiceException { return delegate.listNextBatchOfVersions(previousVersionListing); }
@Override public VersionListing listVersions(String bucketName, String prefix, String keyMarker, String versionIdMarker, String delimiter, Integer maxResults) throws AmazonClientException, AmazonServiceException { return delegate.listVersions(bucketName, prefix, keyMarker, versionIdMarker, delimiter, maxResults); }
@Override public VersionListing listVersions(ListVersionsRequest listVersionsRequest) throws AmazonClientException, AmazonServiceException { return delegate.listVersions(listVersionsRequest); }
@Override public VersionListing listNextBatchOfVersions(ListNextBatchOfVersionsRequest listNextBatchOfVersionsRequest) throws AmazonClientException, AmazonServiceException { return delegate.listNextBatchOfVersions(listNextBatchOfVersionsRequest); }
@Override public VersionListing listNextBatchOfVersions(VersionListing previousVersionListing) throws AmazonClientException, AmazonServiceException { throw new UnsupportedOperationException(); }
@Override public VersionListing listVersions(String bucketName, String prefix) throws AmazonClientException, AmazonServiceException { throw new UnsupportedOperationException(); }
@Override public VersionListing listVersions(String bucketName, String prefix, String keyMarker, String versionIdMarker, String delimiter, Integer maxKeys) throws AmazonClientException, AmazonServiceException { throw new UnsupportedOperationException(); }
@Override public VersionListing listVersions(ListVersionsRequest listVersionsRequest) throws AmazonClientException, AmazonServiceException { throw new UnsupportedOperationException(); }
@Override public VersionListing listNextBatchOfVersions(VersionListing previousVersionListing) throws SdkClientException, AmazonServiceException { return listNextBatchOfVersions(new ListNextBatchOfVersionsRequest(previousVersionListing)); }
@Override public VersionListing listVersions(String bucketName, String prefix) throws SdkClientException, AmazonServiceException { return listVersions(new ListVersionsRequest(bucketName, prefix, null, null, null, null)); }