1

In AWS SDK v1, we could use the getBucketName() method from S3ObjectSummary to retrieve the bucket name. However, in AWS SDK v2, the S3Object class doesn’t seem to provide a similar method.

How can I get the bucket name of an object in AWS SDK v2?

1
  • 2
    Don't you already have the bucket name, since it's needed to download the object? Commented Nov 12 at 12:22

1 Answer 1

1

In AWS SDK v2 the bucket name isn't part of the S3Object anymore; it only provides information about the object itself (key, size, lastModified and so on). The bucket info comes from the request you made. For example:

ListObjectsV2Response res= s3.listObjectsV2(r -> r.bucket("bucket_name"));

You already know the bucket name from that call, so when looping:

String bucket ="bucket_name";
for (S3Object o : res.contents()) {  
System.out.println(bucket + "/" + o.key());
}

If you’re just working with a single object (like GetObjectResponse), it's the same idea: you give the bucket name when calling getObject() and reuse it.

Documentation:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.