Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I easily determine if a Boto 3 S3 bucket resource exists?

For example, I have this code:

import boto3  s3 = boto3.resource('s3')  bucket = s3.Bucket('my-bucket-name')  # Does it exist??? 
like image 421
Daniel Avatar asked Nov 11 '14 18:11

Daniel


People also ask

How do you check if an object exists in S3 using boto3?

Boto3 resource doesn't provide any method directly to check if the key exists in the S3 bucket. Hence, you can load the S3 object using the load() method. If there is no exception thrown, then the key exists. If there is a client error thrown and the error code is 404 , then the key doesn't exist in the bucket.

How do I know if my S3 bucket is empty?

1 Answer. Show activity on this post. IsFolderEmpty (Virtual Folder: S3 does not have folder) : Request for Prefix="Folder" and delimiter=null - Check if listResponse's S3 Object Count is zero thats means Folder having prefix is empty or file(s) having prefix does not exist.

What is S3 boto3 resource (' S3 ')?

Amazon Simple Storage Service (Amazon S3) is object storage commonly used for data analytics applications, machine learning, websites, and many more. To start programmatically working with Amazon S3, you must install the AWS Software Development Kit (SDK).


1 Answers

At the time of this writing there is no high-level way to quickly check whether a bucket exists and you have access to it, but you can make a low-level call to the HeadBucket operation. This is the most inexpensive way to do this check:

from botocore.client import ClientError  try:     s3.meta.client.head_bucket(Bucket=bucket.name) except ClientError:     # The bucket does not exist or you have no access. 

Alternatively, you can also call create_bucket repeatedly. The operation is idempotent, so it will either create or just return the existing bucket, which is useful if you are checking existence to know whether you should create the bucket:

bucket = s3.create_bucket(Bucket='my-bucket-name') 

As always, be sure to check out the official documentation.

Note: Before the 0.0.7 release, meta was a Python dictionary.

like image 94
Daniel Avatar answered Sep 17 '22 14:09

Daniel