0

I'm trying to check if a bucket exists on s3 and have been following this link: https://stackoverflow.com/a/49817544/19505278


s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket-name')

if bucket.creation_date:
   print("The bucket exists")
else:
   print("The bucket does not exist")

However, I'm unable to get this to work due to a potential missing permission.

I was able to try this on a different s3 bucket and can verify this works. However, the s3 bucket I'm working with does not and is likely due to missing permissions. Unfortunately, I do not have access to the working bucket's permissions.

Is there a permission that I need to enable to retrieve bucket metadata?

2 Answers2

0

Here is how you would typically test for the existence of an S3 bucket:

import boto3
from botocore.exceptions import ClientError

Bucket = "my-bucket"

s3 = boto3.client("s3")

try:
    response = s3.head_bucket(Bucket=Bucket)
    print("The bucket exists")
except ClientError as e:
    if e.response["Error"]["Code"] == "404":
        print("No such bucket")
    elif e.response["Error"]["Code"] == "403":
        print("Access denied")
    else:
        print("Unexpected error:", e)
jarmod
  • 71,565
  • 16
  • 115
  • 122
  • does the boto3.client take in the same args as boto3.resource? – dazedconfused21 Feb 02 '23 at 20:12
  • They're both described at [Boto3 reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/boto3.html) but, this being Python, you'll struggle to find a definitive reference document. I believe that `resource(*args, **kwargs)` and `client(*args, **kwargs)` take the same parameters, based on reading the relevant parts of the [session source code](https://github.com/boto/boto3/blob/develop/boto3/session.py). – jarmod Feb 02 '23 at 20:45
0

If you think that there is a permission issue, you might want to check the documentation on permissions on s3. If you simply want to make sure you can check existence of all buckets, s3:ListAllMyBuckets would work nicely.

For the code, you usually want to make it light-weight by using head_bucket for buckets, head_object for objects etc. @jarmod above provided sample code.

As for question on client vs resource, client is close to metal i.e. actual back-end api powering the service. Resource is higher level. It tries to create meaningful objects that you would create from client response. They both use botocore underneath. There are sometimes slight differences when requesting something as resource would already have the knowledge of underlying object.

For example, if you first create a Bucket Resource object, you can simply use a method that's meaningful for that bucket without specifying Bucket Name again.

resource = boto3.resource('s3')
bucket = resource.Bucket('some_bucket_name')

# you can do stuff with this bucket, e.g. create it without supplying any params
bucket.create()

# if you are using client, story is different. You dont have access to objects, so you need to supply everything

client = boto3.client('s3')
client.create_bucket(BucketName='some_bucket_name')

# here you would need to supply 
client.create_bucket()
Adil Hindistan
  • 6,351
  • 4
  • 25
  • 28