12

How can I check if the bucket already exists in my Aws S3 account using Java SDK?

Using below code

        AmazonS3ClientBuilder.defaultClient().doesBucketExistV2(bucketName);

Checks global existence of bucket and returns true if a bucket with this name exists globally even if I am not the owner of this bucket or I don't have access to that bucket.

I understand the intent for making this method this way so that It allows us to determine the availability of bucket name but this is not what I need. Of course, it will throw exception that I don't have access to it later, but it returns stating that bucket with this name exists.

I want to check if the bucket with the given name exists in my S3 account so that I can perform operations on it.

One possible solution for it can be to list all the buckets and search for my bucket in that returned list which I feel is not a good performance-wise (correct me if I am wrong) as there can be hundreds of thousands of buckets and searching in them is not efficient.

How can I determine if a bucket exists in my S3 account not checking global existence?

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
CodeTalker
  • 1,683
  • 2
  • 21
  • 31
  • 2
    Does `HeadBucket` api meet you need? https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadBucket.html – jellycsc Jul 16 '20 at 17:40
  • I don't think "global" it means it returns buckets outside your S3 account, just that they don't have a region like a lot of AWS resources – matt freake Jul 16 '20 at 17:41
  • @mattfreake Basically let's say my S3 account does not have bucket named 'mybucket', the code snippet shared above returns true bacause someone else in the `globe` actually has a bucket named 'mybucket'. It doesn't return me bucket or it's contents ofcourse, but it returns true because a bucket with this name exists. – CodeTalker Jul 16 '20 at 17:45
  • 1
    First of all, you will need permissions on the S3 bucket, otherwise you cannot access the bucket. The challenge will then be whether or not it is actually in the same account as your credentials are for - your credentials could potentially have access to buckets in other accounts, for example. Take a look at `getBucketAcl()`. – jarmod Jul 16 '20 at 17:59
  • @jellycsc Yes, headBucket api is something I was looking for. Thankyou so much. :) – CodeTalker Jul 16 '20 at 18:35

2 Answers2

18

A HeadBucket request does the job.

HeadBucketRequest headBucketRequest = HeadBucketRequest.builder()
            .bucket(bucketName)
            .build();

    try {
        s3Client.headBucket(headBucketRequest);
        return true;
    } catch (NoSuchBucketException e) {
        return false;
    }
Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
Christos
  • 846
  • 3
  • 15
  • 21
0

SDK 1.x does not have a HeadBucketRequest builder nor a NoSuchBucketException.

In case anyone is looking for the answer using AWS SDK for Java 1.x, this works:

private boolean checkAccessToBucket(final String bucketName) {
    try {
        final HeadBucketRequest request = new HeadBucketRequest(bucketName);
        s3Client.headBucket(request);
        return true;
    } catch (AmazonServiceException ex) {
        if (ex.getStatusCode() == 404 | ex.getStatusCode() == 403 || ex.getStatusCode() == 301) {
            return false;
        }
        throw ex;
    } catch (Exception ex) {
        LOGGER.error("Cannot check access", ex);
        throw ex;
    }
}
Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44