use of software.amazon.awssdk.services.s3.model.ObjectIdentifier 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.ObjectIdentifier in project aws-doc-sdk-examples by awsdocs.
the class DeleteMultiObjects method deleteBucketObjects.
// snippet-start:[s3.java2.delete_many_objects.main]
public static void deleteBucketObjects(S3Client s3, String bucketName) {
// Upload three sample objects to the specfied Amazon S3 bucket.
ArrayList<ObjectIdentifier> keys = new ArrayList<>();
PutObjectRequest putOb = null;
ObjectIdentifier objectId = null;
for (int i = 0; i < 3; i++) {
String keyName = "delete object example " + i;
objectId = ObjectIdentifier.builder().key(keyName).build();
putOb = PutObjectRequest.builder().bucket(bucketName).key(keyName).build();
s3.putObject(putOb, RequestBody.fromString(keyName));
keys.add(objectId);
}
System.out.println(keys.size() + " objects successfully created.");
// Delete multiple objects in one request.
Delete del = Delete.builder().objects(keys).build();
try {
DeleteObjectsRequest multiObjectDeleteRequest = DeleteObjectsRequest.builder().bucket(bucketName).delete(del).build();
s3.deleteObjects(multiObjectDeleteRequest);
System.out.println("Multiple objects are deleted!");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.ObjectIdentifier in project ddf by codice.
the class DeleteBean method delete.
public void delete(@Header(AWS2S3Constants.KEY) String s3Key) {
LOGGER.trace("Deleting: {} / {}", s3Bucket, s3Key);
ArrayList<ObjectIdentifier> toDelete = new ArrayList<>();
toDelete.add(ObjectIdentifier.builder().key(s3Key).build());
DeleteObjectsRequest deleteRequest = DeleteObjectsRequest.builder().bucket(s3Bucket).delete(Delete.builder().objects(toDelete).build()).build();
s3Client.deleteObjects(deleteRequest);
}
use of software.amazon.awssdk.services.s3.model.ObjectIdentifier in project aws-doc-sdk-examples by awsdocs.
the class DeleteObjects method deleteBucketObjects.
// snippet-start:[s3.java2.delete_objects.main]
public static void deleteBucketObjects(S3Client s3, String bucketName, String objectName) {
ArrayList<ObjectIdentifier> toDelete = new ArrayList<ObjectIdentifier>();
toDelete.add(ObjectIdentifier.builder().key(objectName).build());
try {
DeleteObjectsRequest dor = DeleteObjectsRequest.builder().bucket(bucketName).delete(Delete.builder().objects(toDelete).build()).build();
s3.deleteObjects(dor);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
System.out.println("Done!");
}
use of software.amazon.awssdk.services.s3.model.ObjectIdentifier in project hazelcast by hazelcast.
the class S3SinkTest method deleteObjects.
@AfterClass
public static void deleteObjects() {
S3Client client = client();
List<ObjectIdentifier> identifiers = listObjects(client).contents().stream().map(S3Object::key).map(key -> ObjectIdentifier.builder().key(key).build()).collect(Collectors.toList());
if (!identifiers.isEmpty()) {
client.deleteObjects(b -> b.bucket(BUCKET).delete(d -> d.objects(identifiers)));
}
int sleepMillis = (int) (SECONDS.toMillis(WAIT_AFTER_CLEANUP_IN_SECS) / 10);
long deadline = System.currentTimeMillis() + SECONDS.toMillis(WAIT_AFTER_CLEANUP_IN_SECS);
int keyCount;
while ((keyCount = listObjects(client).keyCount()) != 0 && System.currentTimeMillis() < deadline) {
logger.info("After sending the object cleanup request to S3, the bucket, " + BUCKET + ", still has " + keyCount + " keys.");
sleepMillis(sleepMillis);
}
if (keyCount == 0) {
logger.info("We finished waiting because we observe that the keys were deleted.");
} else {
logger.warning("There may still be keys in the bucket that are not deleted.\n" + "At our last check, keyCount was " + keyCount + "\n We finished waiting because of the timeout.");
}
}
Aggregations