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

后端 未结 6 1563
滥情空心
滥情空心 2020-12-29 01:34

For example, I have this code:

import boto3

s3 = boto3.resource(\'s3\')

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

# Does it exist???
6条回答
  •  失恋的感觉
    2020-12-29 01:55

    Use lookup Function -> Returns None if bucket Exist

    if s3.lookup(bucketName) is None:
        bucket=s3.create_bucket(bucketName) # Bucket Don't Exist
    else:
        bucket = s3.get_bucket(bucketName) #Bucket Exist
    

提交回复
热议问题