Report this

What is the reason for this report?

[Java] NoSuchKey Error when Listing Image Files on Spaces so I can delete them

Posted on March 10, 2020

I have some 4 test image files on my DO Spaces and I need to delete them programmatically in a Java EE environment. I’m using the AWS (v2) SDK. The Java EE system is running on JBoss EAP 7.2.0.

The 4 test image files are created on the following path on DO Spaces console:

BUCKET_NAME/supplies/test_supply_id/supply_images

So I start with creating a ListObjectsRequest as follows:

String prefix = "supplies/test_supply_id/supply_images";

ListObjectsRequest listObjectsRequest = ListObjectsRequest
                .builder()
                .bucket(BUCKET_NAME)
                .prefix(prefix)
                .build();

Then I capture the result:

ListObjectsResponse listObjectsResponse = s3cDOSpaceClient.listObjects(listObjectsRequest);

List<S3Object> lsObjectsInBucket = loresListObjectsResponse.contents();

However, I get a software.amazon.awssdk.services.s3.model.NoSuchKeyException: null (Service: S3, Status Code: 404, Request ID: txREQUEST_ID) at the following line:

ListObjectsResponse listObjectsResponse = s3cDOSpaceClient.listObjects(listObjectsRequest);

What could I really be doing wrong? I’ve tried different patterns of the prefix, e.g. having a “/” at the end and/or front, but none has worked. It’s quite hard to find some good documentation on this online, infact even AWS Docs, as described here aren’t helping.

How can I resolve this?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

Based on your description, your code seems fine and should list the objects correctly. There might be a few reasons why you’re facing this issue:

  1. Incorrect Bucket Name: Make sure that BUCKET_NAME is correctly set and points to the bucket you’re trying to access.

  2. Incorrect Region: Make sure the region is correctly configured in your S3 client.

  3. Prefix Issue: While you’ve tried variations of your prefix, ensure there’s no leading slash. In your case, prefix should be “supplies/test_supply_id/supply_images”. You may want to check again on DigitalOcean’s spaces console if the path is correct.

  4. Permissions: Check if the API token that you are using is correct.

  5. SDK Version and Configuration: Ensure that your AWS SDK is up-to-date.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.