简体   繁体   中英

Java : How to print the content of s3 bucket

I am using jdk 11 and virtual-host-style-access (AWS SDK for Java version 2) to create/access objects in AWS s3 bucket following:

https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/examples-s3-objects.html#list-object

While I was able to create objects in the the designated bucket, I am not able to print the list of contents/objects in the bucket although, as I checked the permission, everyone is granted to view the objects in the bucket. The error message is:

software.amazon.awssdk.services.s3.model.NoSuchKeyException: The specified key does not exist. (Service: S3, Status Code: 404

This is the way the s3client is created:

    adapterSmsS3Client = S3Client.builder()
        .region(Region.US_WEST_2)
        .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(ACCESS_KEY,SECRET_KEY)))
        .endpointOverride(URI.create(BASE_URL))
        .build();

And this is the way I am trying to print the list:

public static void listBucketObjects(  S3Client s3, String bucketName ) {
ListBucketsResponse res1 =  s3.listBuckets();

  ListObjectsRequest listObjects = ListObjectsRequest
          .builder()
          .bucket(BUCKET_NAME)
          .build();

  ListObjectsResponse res = s3.listObjects(listObjects);
  List<S3Object> objects = res.contents();

  for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) {
    S3Object myValue = (S3Object) iterVals.next();
    System.out.print("\n The name of the key is " + myValue.key());
    System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
    System.out.print("\n The owner is " + myValue.owner());
  }

}

BUCKET_NAME is the name of bucket on s3 ( not any URL)

Although, I would like to mention that if I use Path-style-request (AWS SDK for Java version 1), following:

https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-s3-objects.html

I am able to print contents from the same bucket. However we do not intend to go that way.

Any insight on why am I getting the "key does not exist" error or potential resolution?

If you had any problem with permissions you would have get a 403 forbidden ; not a 404 NoSuchKey .

What are the names of your objects in the bucket? My guess is that you have some special characters or url-encoded characters that causes the problem. See https://aws.amazon.com/premiumsupport/knowledge-center/404-error-nosuchkey-s3/?nc1=h_ls for more details.

And I suggest you to use listObjectsV2 instead of the V1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM