How do I upload a file to s3 using boto3 in python on heroku? How do I upload a file to s3 using boto3 in python on heroku? heroku heroku

How do I upload a file to s3 using boto3 in python on heroku?


Put your keys in the keyfile according to the quickstart guide and then use the following code:

       import boto3       s3 = boto3.resource(service_name='s3', region_name='REGION_NAME')       data = open('PATH_TO_FILE_ON_DISK', 'rb')       s3.Bucket('BUCKET_NAME').put_object(Key='FILENAME_ON_S3', Body=data)

The official documentation does not make it clear that the region name is required or else you may get an error, and it's not possible to store the configuration file for the region on heroku. It must be included in the resource call.


Since heroku doesn't have access to your AWS config file, you'll have to use environment variables. You can then use any of the various s3 file upload methods.

from os import getenvimport boto3boto_kwargs = {    "aws_access_key_id": getenv("AWS_ACCESS_KEY_ID"),    "aws_secret_access_key": getenv("AWS_SECRET_ACCESS_KEY"),    "region_name": getenv("AWS_REGION"),}s3_client = boto3.Session(**boto_kwargs).client("s3")s3_client.upload_fileobj(<f>, <bucket_name>, <object_name>)


Just for reference here's how you can fix the issue of v4 authentication being used for all new regions - set signature_version in the config file:

--- .aws/config ---[default]output = json[profile myprofile]region = REGION_NAMEs3=  signature_version = s3#  addressing_style = path--- .aws/credentials ---[myprofile]aws_access_key_id = <access-key>aws_secret_access_key = <secret-key>--- python ---import boto3session = boto3.Session(profile_name='myprofile')s3 = session.resource('s3')with open('PATH_TO_FILE_ON_DISK', 'rb') as data:    s3.Object('BUCKET_NAME', 'FILENAME_ON_S3').put(Body=data)

Alternatively:

s3.Object('BUCKET_NAME', 'FILENAME_ON_S3').upload_file('PATH_TO_FILE_ON_DISK')