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()

--

--

pi314tech
pi314tech

Written by pi314tech

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

No responses yet

Write a response