Get #Amazon #Prime for this #holiday #amazonprime #christmas #2019

How to upload a zip file to #aws #s3 using #boto3? #python

By Jun - Support me on Amazon

According to boto3 document, these are the methods that are available for uploading.


The managed upload methods are exposed in both the client and resource interfaces of boto3:
* S3.Client method to upload a file by name: S3.Client.upload_file()
* S3.Client method to upload a readable file-like object: S3.Client.upload_fileobj()
* S3.Bucket method to upload a file by name: S3.Bucket.upload_file()
* S3.Bucket method to upload a readable file-like object: S3.Bucket.upload_fileobj()
* S3.Object method to upload a file by name: S3.Object.upload_file()
* S3.Object method to upload a readable file-like object: S3.Object.upload_fileobj()
(The above methods and note are taken from boto3 doc, and there is note saying that they are the same method for different S3 classes.)

What I used was 

The method definition is

upload_file(Filename, Bucket, Key, ExtraArgs=None, Callback=None, Config=None)
- Upload a file to an S3 object.

S3 boto3 upload_file doc

Example:

import boto3
s3Resource = boto3.resource('s3')

try:
    s3Resource.meta.client.upload_file('/path/to/file', 'bucketName', 'keyName')
except Exception as err:
    print(err)

You can use ExtraArgs parameter to set ACL, metadata, content-encoding etc.

import boto3
s3Resource = boto3.resource('s3')

try:
    s3Resource.meta.client.upload_file(
        '/path/to/file',
        'bucketName',
        'keyName',
        ExtraArgs={'ACL': 'public-read'})
except Exception as err:
    print(err)

All the valid extra arguments are listed on this boto3 doc. I have them copied below for easier reference.

ALLOWED_UPLOAD_ARGS = ['ACL', 'CacheControl', 'ContentDisposition',   
'ContentEncoding', 'ContentLanguage', 'ContentType', 'Expires', 
'GrantFullControl', 'GrantRead', 'GrantReadACP', 'GrantWriteACP', 'Metadata',
'RequestPayer', 'ServerSideEncryption', 'StorageClass','SSECustomerAlgorithm', 'SSECustomerKey', 'SSECustomerKeyMD5', 'SSEKMSKeyId', 'WebsiteRedirectLocation']

If you need help with boto3, you can join their gitter channel.

Resources:

1) What is ExtraArgs for `upload_fileobj`? boto3 GitHub thread
2) Boto3 not uploading zip file to S3 python StackOverflow thread
3) python: Open file from zip without temporary extracting it StackOverflow thread

Thanks for reading!