In this tutorial we will learn how to upload excel file to Amazon S3 bucket in python using Amazon's SDK Boto3. To complete this tutorial, we need to perform following steps.
- Get you access key and secret key from AWS management console.
- Install boto3 module.
- Write python code to upload file to Amazon S3 bucket.
1. Get your access key and secret key from AWS management console.
We assume you already have a Amazon Web Services account. If you don't have, create new account.
- Login to AWS management console.
- Click on your username at the top-right of the page to open the drop-down menu.
- Click on My Security Credentials. (Your Security Credential tab will open by default)
- Click on the Access keys (access key ID and secret access key)
- To create a new access key and secret, click on the Create New Access Key button.
- Download the .csv file containing your access key and secret.
2. Install boto3 module
Use pip as your package installer, run the below command in cmd.
pip install boto3
If you are working in virtual environment then make sure to activate your virtual environment first.
3. Write python code to upload file to Amazon S3 bucket.
# import module
import boto3
from botocore.exceptions import ClientError
# declare constants
AWS_ACCESS_KEY = "xxxxxxxxxx"
AWS_SECRET_KEY = "xxxxxxxxxxxxxxxxxxxxxxxx"
S3_BUCKET = 'bucket_name'
def upload_file_to_s3(file_name, bucket, object_name):
"""
:param file_name: file name to upload
:param bucket: S3 bucket
:param object_name: S3 object name
:return: True if successful upload else False
"""
s3 = boto3.client("s3", aws_access_key_id=AWS_ACCESS_KEY,
aws_secret_access_key=AWS_SECRET_KEY)
try:
s3.upload_fileobj(
file_name,
bucket,
object_name,
ExtraArgs={"ContentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"ContentDisposition": "attachment"}
)
except ClientError as e:
return False
return True
upload_file_to_s3(local_file_name, S3_BUCKET, s3_object_name)