Saturday, December 2, 2017

com.amazonaws.services.s3.model.AmazonS3Exception: User key must be specified

Question:

com.amazonaws.services.s3.model.AmazonS3Exception: User key must be specified. (Service: Amazon S3; Status Code: 400;
       at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1160)
       at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:748)
       at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:467)
       at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:302)
       at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3769)
       at com.amazonaws.services.s3.AmazonS3Client.deleteObjects(AmazonS3Client.java:1841)
The code looks like below:
client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys.toArray(new String[urls.length]))
access/secret keys are set up properly.
What is the user key?

Answer:

What is User Key?
Ans:
User Key is a key by which user can get credentials. It verifies who you are and whether you have permission to access the resources that you are requesting. User security credentials have Access Key ID and Secret Access Key.
You must store the keys in secure location. Your secret key will no longer be available through the AWS Management Console; you will have the only copy. Keep it confidential in order to protect your account, and never email it. Do not share it outside your organization, even if an inquiry appears to come from AWS or Amazon.com. No one who legitimately represents Amazon will ever ask you for your secret key.
Your client.deleteObjects(....); methods have keys which are processing using array. But ArrayList is used below for deleting objects with proper exception handling.
Deleting Multiple Objects (Version-Enabled Bucket)
  1. Create an instance of the AmazonS3Client class.
  2. Create an instance of the DeleteObjectsRequest class and provide a list of objects keysand optionally the version IDs of the objects that you want to delete.
    If you specify the version ID of the object that you want to delete, Amazon S3 deletes the specific object version. If you don't specify the version ID of the object that you want to delete, Amazon S3 adds a delete marker. For more information, see Deleting One Object Per Request.
  3. Execute the AmazonS3Client.deleteObjects method.
The following Java code sample demonstrates the preceding steps.
List<KeyVersion> keys = new ArrayList<KeyVersion>();
// Provide a list of object keys and versions.

DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName)
.withKeys(keys);

try {
    DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest);
    System.out.format("Successfully deleted all the %s items.\n", delObjRes.getDeletedObjects().size());

} catch (MultiObjectDeleteException e) {
    // Process exception.
}
For Multi-Object Delete (Non-Versioned Bucket) you can use this method also
static void multiObjectNonVersionedDelete(List<KeyVersion> keys) {

    // Multi-object delete by specifying only keys (no version ID).
    DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(
            bucketName).withQuiet(false);

    // Create request that include only object key names.
    List<KeyVersion> justKeys = new ArrayList<KeyVersion>();
    for (KeyVersion key : keys) {
        justKeys.add(new KeyVersion(key.getKey()));
    }
    multiObjectDeleteRequest.setKeys(justKeys);
    // Execute DeleteObjects - Amazon S3 add delete marker for each object
    // deletion. The objects no disappear from your bucket (verify).
    DeleteObjectsResult delObjRes = null;
    try {
        delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest);
        System.out.format("Successfully deleted all the %s items.\n", delObjRes.getDeletedObjects().size());
    } catch (MultiObjectDeleteException mode) {
        printDeleteResults(mode);
    }
}
For details with example, you can follow the tutorials
  1. Deleting Multiple Objects Using the AWS SDK for Java
  2. Deleting an Object Using the AWS SDK for Java
  3. Deleting Objects

No comments:

Post a Comment