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?
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?
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: