Feedback
Do you have a suggestion to improve this website or boto3?Give us feedback.
The AWS SDK for Python provides a pair of methods to upload a file to an S3bucket.
Theupload_file
method accepts a file name, a bucket name, and an objectname. The method handles large files by splitting them into smaller chunksand uploading each chunk in parallel.
importloggingimportboto3frombotocore.exceptionsimportClientErrorimportosdefupload_file(file_name,bucket,object_name=None):"""Upload a file to an S3 bucket :param file_name: File to upload :param bucket: Bucket to upload to :param object_name: S3 object name. If not specified then file_name is used :return: True if file was uploaded, else False """# If S3 object_name was not specified, use file_nameifobject_nameisNone:object_name=os.path.basename(file_name)# Upload the files3_client=boto3.client('s3')try:response=s3_client.upload_file(file_name,bucket,object_name)exceptClientErrorase:logging.error(e)returnFalsereturnTrue
Theupload_fileobj
method accepts a readable file-like object. The fileobject must be opened in binary mode, not text mode.
s3=boto3.client('s3')withopen("FILE_NAME","rb")asf:s3.upload_fileobj(f,"amzn-s3-demo-bucket","OBJECT_NAME")
Theupload_file
andupload_fileobj
methods are provided by the S3Client
,Bucket
, andObject
classes. The method functionalityprovided by each class is identical. No benefits are gained by calling oneclass’s method over another’s. Use whichever class is most convenient.
Bothupload_file
andupload_fileobj
accept an optionalExtraArgs
parameter that can be used for various purposes. The list of validExtraArgs
settings is specified in theALLOWED_UPLOAD_ARGS
attributeof theS3Transfer
objectatboto3.s3.transfer.S3Transfer.ALLOWED_UPLOAD_ARGS
.
The followingExtraArgs
setting specifies metadata to attach to the S3object.
s3.upload_file('FILE_NAME','amzn-s3-demo-bucket','OBJECT_NAME',ExtraArgs={'Metadata':{'mykey':'myvalue'}})
The followingExtraArgs
setting assigns the canned ACL (access controllist) value ‘public-read’ to the S3 object.
s3.upload_file('FILE_NAME','amzn-s3-demo-bucket','OBJECT_NAME',ExtraArgs={'ACL':'public-read'})
TheExtraArgs
parameter can also be used to set custom or multiple ACLs.
s3.upload_file('FILE_NAME','amzn-s3-demo-bucket','OBJECT_NAME',ExtraArgs={'GrantRead':'uri="http://acs.amazonaws.com/groups/global/AllUsers"','GrantFullControl':'id="01234567890abcdefg"',})
Bothupload_file
andupload_fileobj
accept an optionalCallback
parameter. The parameter references a class that the Python SDK invokesintermittently during the transfer operation.
Invoking a Python class executes the class’s__call__
method. For eachinvocation, the class is passed the number of bytes transferred upto that point. This information can be used to implement a progress monitor.
The followingCallback
setting instructs the Python SDK to create aninstance of theProgressPercentage
class. During the upload, theinstance’s__call__
method will be invoked intermittently.
s3.upload_file('FILE_NAME','amzn-s3-demo-bucket','OBJECT_NAME',Callback=ProgressPercentage('FILE_NAME'))
An example implementation of theProcessPercentage
class is shown below.
importosimportsysimportthreadingclassProgressPercentage(object):def__init__(self,filename):self._filename=filenameself._size=float(os.path.getsize(filename))self._seen_so_far=0self._lock=threading.Lock()def__call__(self,bytes_amount):# To simplify, assume this is hooked up to a single filenamewithself._lock:self._seen_so_far+=bytes_amountpercentage=(self._seen_so_far/self._size)*100sys.stdout.write("\r%s%s /%s (%.2f%%)"%(self._filename,self._seen_so_far,self._size,percentage))sys.stdout.flush()