Home > Software engineering >  List specific objects in AWS S3 SDK
List specific objects in AWS S3 SDK

Time:05-28

I'm trying to delete multiple objects in a bucket using SDK, but when i pass the list of KeyVersions to the method .deleteObjects(deleteObjectsRequest), he actually deletes the objects from my list, but if i pass a list that contain one or more objects that aren't inside the Bucket it deletes the objects found and does not return which objects weren't deleted because wasn't found.

private void deleteMultipleObjectsFromS3(final List<KeyVersion> objectsKeys) {
    final DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(this.bucketName)
            .withKeys(objectsKeys)
            .withQuiet(false);

    try {
          s3Client.deleteObjects(deleteObjectsRequest);
        } catch (final AmazonServiceException e) {
          throw new AmazonSdkException(e, e.getErrorMessage(), DELETE_OPERATION, 
          this.bucketName, objectsKeys.toString());
        } catch (SdkClientException e) {
          throw new AmazonSdkException(e, e.getLocalizedMessage(), DELETE_OPERATION, this.bucketName, objectsKeys.toString());
    }
  }

The response get all objects that i passed, i need to inform in the endpoint which objects were, or not, deleted.

enter image description here

I'm using Java SDK version 1.12.181

EDIT 1:

I've also tried to get the objects sumaries, but it returns all objects in the bucket. What i don't want, because some buckets that i'll operate might have more than 100.000 objects.

final ListObjectsV2Result listObjectsV2Result = s3Client.listObjectsV2(listObjectsRequest);
final List<S3ObjectSummary> s3ObjectSummary = listObjectsV2Result.getObjectSummaries();

CodePudding user response:

DeleteItem will still return a successful response if the object does not exist. This is irrespective of SDK version or language as this response is returned by the underlying S3 API.

If you need to know an object has been deleted or not, since you're on the v1 of the Java SDK:

  1. For every object, call doesObjectExist passing in the bucket name & object name

  2. If true is returned, add to a toBeDeleted list

  3. If false is returned, add to a notDeleted list

  4. Call deleteObjects for the items in toBeDeleted

  5. If any items failed to be deleted, remove from toBeDeleted & add to notDeleted

Anything in notDeleted, was not deleted & anything in toBeDeleted was deleted.

  • Related