Issue
I am using AWS S3 bucket for my project where I am uploading images and listing images using APIs, which is working pretty fine.
Now I want to list all files of a specific S3 bucket/folder (listing of objects of the specific bucket).
Here is the screenshot of my S3 bucket:
I tried to give bucket names like
wevieu/development/user_default/
wevieu/development/user_default
s3://wevieu/development/user_default/
etc. but nothing helped me.
Most of the time I am getting this error or any other error with 400 response:
The specified key does not exist. (Service: Amazon S3; Status Code: 404; Error Code: NoSuchKey;
I tried 1 , 2, 3 solutions but didn't get help from any of this.
Help me if anyone have done this before successfully.
Note: I am using this SDK version com.amazonaws:aws-java-sdk-s3:1.11.423
Solution
Ok. So I got the solution.
You need to add the permissions before listing objects in your S3. If permissions are missing then your code will give you errors.
The code that I did use is this, which was not working before adding permission.
After adding permissions, I am able to list all objects.
objectListing = s3client.listObjects("wevieu", "development/user_default");
if (objectListing != null) {
List<S3ObjectSummary> s3ObjectSummariesList = objectListing.getObjectSummaries();
if (!s3ObjectSummariesList.isEmpty()) {
for (S3ObjectSummary objectSummary : s3ObjectSummariesList) {
System.out.println("file name:"+objectSummary.getKey());
}
}
}
Answered By - Kishan Solanki
Answer Checked By - Cary Denson (JavaFixing Admin)