use of software.amazon.awssdk.services.s3.model.Delete in project beam by apache.
the class S3FileSystem method delete.
private void delete(String bucket, Collection<String> keys) throws IOException {
checkArgument(keys.size() <= MAX_DELETE_OBJECTS_PER_REQUEST, "only %s keys can be deleted per request, but got %s", MAX_DELETE_OBJECTS_PER_REQUEST, keys.size());
List<ObjectIdentifier> deleteKeyVersions = keys.stream().map((key) -> ObjectIdentifier.builder().key(key).build()).collect(Collectors.toList());
Delete delete = Delete.builder().objects(deleteKeyVersions).quiet(true).build();
DeleteObjectsRequest deleteObjectsRequest = DeleteObjectsRequest.builder().bucket(bucket).delete(delete).build();
try {
s3Client.get().deleteObjects(deleteObjectsRequest);
} catch (SdkServiceException e) {
throw new IOException(e);
}
}
use of software.amazon.awssdk.services.s3.model.Delete in project beam by apache.
the class S3FileSystemTest method deleteThousandsOfObjectsInMultipleBucketsWithS3Options.
@Test
public void deleteThousandsOfObjectsInMultipleBucketsWithS3Options() throws IOException {
S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Options());
List<String> buckets = ImmutableList.of("bucket1", "bucket2");
List<String> keys = new ArrayList<>();
for (int i = 0; i < 2500; i++) {
keys.add(String.format("key-%d", i));
}
List<S3ResourceId> paths = new ArrayList<>();
for (String bucket : buckets) {
for (String key : keys) {
paths.add(S3ResourceId.fromComponents("s3", bucket, key));
}
}
s3FileSystem.delete(paths);
// Should require 6 calls to delete 2500 objects in each of 2 buckets.
verify(s3FileSystem.getS3Client(), times(6)).deleteObjects(any(DeleteObjectsRequest.class));
}
use of software.amazon.awssdk.services.s3.model.Delete in project aws-doc-sdk-examples by awsdocs.
the class S3BucketDeletion method main.
public static void main(String[] args) throws Exception {
final String USAGE = "\n" + "Usage:\n" + " <bucket>\n\n" + "Where:\n" + " bucket - the bucket to delete (for example, bucket1). \n\n";
if (args.length != 1) {
System.out.println(USAGE);
System.exit(1);
}
String bucket = args[0];
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();
listAllObjects(s3, bucket);
s3.close();
}
use of software.amazon.awssdk.services.s3.model.Delete in project aws-doc-sdk-examples by awsdocs.
the class S3BucketDeletion method listAllObjects.
// snippet-start:[s3.java2.s3_bucket_ops.delete_bucket]
public static void listAllObjects(S3Client s3, String bucket) {
try {
// To delete a bucket, all the objects in the bucket must be deleted first
ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket).build();
ListObjectsV2Response listObjectsV2Response;
do {
listObjectsV2Response = s3.listObjectsV2(listObjectsV2Request);
for (S3Object s3Object : listObjectsV2Response.contents()) {
s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(s3Object.key()).build());
}
listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket).continuationToken(listObjectsV2Response.nextContinuationToken()).build();
} while (listObjectsV2Response.isTruncated());
// snippet-end:[s3.java2.s3_bucket_ops.delete_bucket]
DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build();
s3.deleteBucket(deleteBucketRequest);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.Delete in project aws-doc-sdk-examples by awsdocs.
the class S3Cors method setCorsInformation.
public static void setCorsInformation(S3Client s3, String bucketName, String accountId) {
List<String> allowMethods = new ArrayList();
allowMethods.add("PUT");
allowMethods.add("POST");
allowMethods.add("DELETE");
List<String> allowOrigins = new ArrayList();
allowOrigins.add("http://example.com");
try {
// Define CORS rules.
CORSRule corsRule = CORSRule.builder().allowedMethods(allowMethods).allowedOrigins(allowOrigins).build();
List<CORSRule> corsRules = new ArrayList<>();
corsRules.add(corsRule);
CORSConfiguration configuration = CORSConfiguration.builder().corsRules(corsRules).build();
PutBucketCorsRequest putBucketCorsRequest = PutBucketCorsRequest.builder().bucket(bucketName).corsConfiguration(configuration).expectedBucketOwner(accountId).build();
s3.putBucketCors(putBucketCorsRequest);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
Aggregations