Loading...

django tutorial

Upload a file to S3 using boto3 in django

Install:

pip3 install boto3

Set region and credentials

First we need to select the region where the bucket is placed and your account credentials.

Using Account credentials isn’t a good practise as they give full access to AWS resources http://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html?icmpid=docs_iam_console

REGION = 'us-east-1' 
ACCESS_KEY_ID = 'paste_here_your_key_id' 
SECRET_ACCESS_KEY = 'paste_here_your_secret_access_key'

Select file to upload (computer)

PATH_IN_COMPUTER = 'path/in/computer/namefile.txt'

Select file destination (AWS S3)

BUCKET_NAME = 'vperezb' 
KEY = 'path/in/s3/namefile.txt' # file path in S3

Upload the file to S3

s3_resource = boto3.resource(
    's3', 
    region_name = REGION,
    aws_access_key_id = ACCESS_KEY_ID, 
    aws_secret_access_key = SECRET_ACCESS_KEY
)
s3_resource.Bucket(BUCKET_NAME).put_object(
    Key = KEY, 
    Body = open(PATH_IN_COMPUTER, 'rb')
)

All togeather

import boto3 REGION = 'us-east-1' 
ACCESS_KEY_ID = 'paste_here_your_key_id' 
SECRET_ACCESS_KEY = 'paste_here_your_secret_access_key' 
PATH_IN_COMPUTER = 'path/in/computer/namefile.txt' 
BUCKET_NAME = 'vperezb' 
KEY = 'path/in/s3/namefile.txt' # file path in S3 

s3_resource = boto3.resource(
    's3', 
    region_name = REGION, 
    aws_access_key_id = ACCESS_KEY_ID,
    aws_secret_access_key = SECRET_ACCESS_KEY
)

s3_resource.Bucket(BUCKET_NAME).put_object(
    Key = KEY, 
    Body = open(PATH_IN_COMPUTER, 'rb')
)