Member-only story

Python script to upload file to AWS s3

pi314tech
2 min readJul 1, 2021

--

Python script to upload file to AWS s3

This article will help you to upload a file to AWS S3. This will be a handy script to push up a file to s3 bucket that you have access to.

I use MacOS, so all the commands are relative to MacOS operating system.

Before getting started

Check Python version and install Python if it is not installed.

python3 --version
Python 3.9.1

Now create a new file named `upload-to-s3.py`

#!/usr/bin/env python3print("import to s3 script")import boto3
import datetime
from botocore.client import Config
def file_ts():
return datetime.datetime.today().strftime('%Y-%m-%d-%H:%M')
def read_in_file(filePath):
return open(filePath, 'r')
def fileName():
return "test".join((file_ts(), '.csv'))
def execute():
aws_access_key_id = "PUT_YOUR_AWS_ACCESS_KEY"
aws_secret_access_key = "PUT_YOUR_AWS_ACCESS_KEY_SECRET"
s3_bucket_name = "PUT_YOUR_S3_BUCKET_NAME"
#f = read_in_file('test.csv')
s3 = boto3.resource('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, config=Config(signature_version='s3v4'))
s3.Bucket(s3_bucket_name).upload_file('test.csv', fileName())
execute()

Before running the script:

Before you run the script check if `boto3` module installed.

pip show boto3

In case the pip package is not installed, install boto3

pip3 install boto3

Sample output:

Collecting boto3
Downloading boto3-1.17.103-py2.py3-none-any.whl (131 kB)
|████████████████████████████████| 131 kB 287 kB/s
Collecting jmespath<1.0.0,>=0.7.1
Downloading jmespath-0.10.0-py2.py3-none-any.whl (24 kB)
Collecting s3transfer<0.5.0,>=0.4.0
Downloading s3transfer-0.4.2-py2.py3-none-any.whl (79 kB)
#!/usr/bin/py
|████████████████████████████████| 79 kB 112 kB/s
Collecting botocore<1.21.0,>=1.20.103
Downloading botocore-1.20.103-py2.py3-none-any.whl (7.7 MB)
|████████████████████████████████| 7.7 MB 449 kB/s
Collecting python-dateutil<3.0.0,>=2.1
Downloading python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)…

--

--

pi314tech
pi314tech

Written by pi314tech

Technology enthusiast, lifelong learner, developer, photographer and travel blogger

No responses yet

Write a response