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!
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:
Incorrect Bucket Name: Make sure that BUCKET_NAME is correctly set and points to the bucket you’re trying to access.
Incorrect Region: Make sure the region is correctly configured in your S3 client.
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.
Permissions: Check if the API token that you are using is correct.
SDK Version and Configuration: Ensure that your AWS SDK is up-to-date.
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.