Amazon S3 Buckets by Boto3
Amazon S3 Buckets by Boto3
import logging
import boto3
region (us-east-1).
"""
# Create bucket
try:
if region is None:
s3_client = boto3.client('s3')
s3_client.create_bucket(Bucket=bucket_name)
else:
s3_client.create_bucket(Bucket=bucket_name,
CreateBucketConfiguration=location)
except ClientError as e:
logging.error(e)
return False
return True
s3 = boto3.client('s3')
response = s3.list_buckets()
print('Existing buckets:')
print(f' {bucket["Name"]}')
Uploading files
The AWS SDK for Python provides a pair of methods to upload a file to an S3 bucket.
The upload_file method accepts a file name, a bucket name, and an object name.
The method handles large files by splitting them into smaller chunks and uploading
each chunk in parallel.
import logging
import boto3
import os
"""
if object_name is None:
object_name = os.path.basename(file_name)
# Upload the file
s3_client = boto3.client('s3')
try:
except ClientError as e:
logging.error(e)
return False
return True
The upload_fileobj method accepts a readable file-like object. The file object must
be opened in binary mode, not text mode.
s3 = boto3.client('s3')
s3.upload_file(
)
The following ExtraArgs setting assigns the canned ACL (access control list) value
'public-read' to the S3 object.
s3.upload_file(
ExtraArgs={'ACL': 'public-read'}
The ExtraArgs parameter can also be used to set custom or multiple ACLs.
s3.upload_file(
ExtraArgs={
'GrantRead':
'uri="http://acs.amazonaws.com/groups/global/AllUsers"',
'GrantFullControl': 'id="01234567890abcdefg"',
s3.upload_file(
Callback=ProgressPercentage('FILE_NAME')
)
import os
import sys
import threading
class ProgressPercentage(object):
self._filename = filename
self._size = float(os.path.getsize(filename))
self._seen_so_far = 0
self._lock = threading.Lock()
with self._lock:
self._seen_so_far += bytes_amount
sys.stdout.write(
"\r%s %s / %s (%.2f%%)" % (
percentage))
sys.stdout.flush()
Downloading files
The methods provided by the AWS SDK for Python to download files are similar to
those provided to upload files.
The download_file method accepts the names of the bucket and object to download
and the filename to save the file to.
import boto3
s3 = boto3.client('s3')
The download_fileobj method accepts a writeable file-like object. The file object
must be opened in binary mode, not text mode.
s3 = boto3.client('s3')
s3.download_fileobj('BUCKET_NAME', 'OBJECT_NAME', f)
Like their upload cousins, the download methods are provided by the
S3 Client, Bucket, and Object classes, and each class provides identical functionality.
Use whichever class is convenient.
Also like the upload methods, the download methods support the
optional ExtraArgs and Callback parameters.
The list of valid ExtraArgs settings for the download methods is specified in
the ALLOWED_DOWNLOAD_ARGS attribute of the S3Transfer object
at boto3.s3.transfer.S3Transfer.ALLOWED_DOWNLOAD_ARGS.
The download method's Callback parameter is used for the same purpose as the
upload method's. The upload and download methods can both invoke the
same Callback class.