Amazon S3 — Boto 3 Docs 1.12.6 documentation
Boto 2.x contains a number of customizations to make working with Amazon S3 buckets and keys easy. Boto 3 exposes these same objects through its resources interface in a unified and consistent way.
Creating the Connection
Boto 3 has both low-level clients and higher-level resources. For Amazon S3, the higher-level resources are the most similar to Boto 2.x's s3 module:
# Boto 2.ximport botos3_connection = boto.connect_s3()# Boto 3import boto3s3 = boto3.resource('s3')
Creating a Bucket
Creating a bucket in Boto 2 and Boto 3 is very similar, except that in Boto 3 all action parameters must be passed via keyword arguments and a bucket configuration must be specified manually:
# Boto 2.xs3_connection.create_bucket('mybucket')s3_connection.create_bucket('mybucket', location=Location.USWest)# Boto 3s3.create_bucket(Bucket='mybucket')s3.create_bucket(Bucket='mybucket', CreateBucketConfiguration={ 'LocationConstraint': 'us-west-1'})
Storing Data
Storing data from a file, stream, or string is easy:
# Boto 2.xfrom boto.s3.key import Keykey = Key('hello.txt')key.set_contents_from_file('/tmp/hello.txt')# Boto 3s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))
Accessing a Bucket
Getting a bucket is easy with Boto 3's resources, however these do not automatically validate whether a bucket exists:
# Boto 2.xbucket = s3_connection.get_bucket('mybucket', validate=False)exists = s3_connection.lookup('mybucket')# Boto 3import botocorebucket = s3.Bucket('mybucket')exists = Truetry: s3.meta.client.head_bucket(Bucket='mybucket')except botocore.exceptions.ClientError as e: # If a client error is thrown, then check that it was a 404 error. # If it was a 404 error, then the bucket does not exist. error_code = e.response['Error']['Code'] if error_code == '404': exists = False
Deleting a Bucket
All of the keys in a bucket must be deleted before the bucket itself can be deleted:
# Boto 2.xfor key in bucket: key.delete()bucket.delete()# Boto 3for key in bucket.objects.all(): key.delete()bucket.delete()
Iteration of Buckets and Keys
Bucket and key objects are no longer iterable, but now provide collection attributes which can be iterated:
# Boto 2.xfor bucket in s3_connection: for key in bucket: print(key.name)# Boto 3for bucket in s3.buckets.all(): for key in bucket.objects.all(): print(key.key)
Access Controls
Getting and setting canned access control values in Boto 3 operates on an ACL resource object:
# Boto 2.xbucket.set_acl('public-read')key.set_acl('public-read')# Boto 3bucket.Acl().put(ACL='public-read')obj.Acl().put(ACL='public-read')
It's also possible to retrieve the policy grant information:
# Boto 2.xacp = bucket.get_acl()for grant in acp.acl.grants: print(grant.display_name, grant.permission)# Boto 3acl = bucket.Acl()for grant in acl.grants: print(grant['Grantee']['DisplayName'], grant['Permission'])
Boto 3 lacks the grant shortcut methods present in Boto 2.x, but it is still fairly simple to add grantees:
# Boto 2.xbucket.add_email_grant('READ', '')# Boto 3bucket.Acl.put(GrantRead='emailAddress=')
Managing CORS Configuration
Allows you to manage the cross-origin resource sharing configuration for S3 buckets:
# Boto 2.xcors = bucket.get_cors()config = CORSConfiguration()config.add_rule('GET', '*')bucket.set_cors(config)bucket.delete_cors()# Boto 3cors = bucket.Cors()config = { 'CORSRules': [ { 'AllowedMethods': ['GET'], 'AllowedOrigins': ['*'] } ]}cors.put(CORSConfiguration=config)cors.delete()