While I was trying to upload objects to a specific bucket in Amazon S3 storage via Amazon Web Services API, I obviously needed a reference to that existing bucket. I was using AmazonS3Client
to perform that, however I couldn't see any method such as getBucket(String bucketID)
.
Please check here: AmazonS3Client
I applied a brute-force search to get a reference to the desired bucket:
AmazonS3Client connection = new AmazonS3Client(credentials);
Bucket targetBucket = null;
List<Bucket> buckets = connection.listBuckets();
for (Bucket bucket : buckets)
{
if (bucket.getName().equals(bucketId))
{
targetBucket = bucket;
break;
}
}
Obviously, this works fine. But I wonder why the API doesn't provide such a trivial method or might I be missing something or do you know any other method(s) from the same API which will replace the lines above with one-line code?
EDIT:
For a simple task like uploading a file, the bucket name seems to be sufficient:
if (connection.doesBucketExist(bucketId))
{
connection.putObject(bucketId, ... );
}
However, my question is as the title implies, why isn't there a method returning a reference to a specific Bucket
instance?