How to Upload files to s3 using Python||AWS

In this article, I will be showing you how to upload files from your computer to AWS S3 Bucket using Python...

python || AWS
Photo by Ilya Pavlov on Unsplash

Lets get started

Before starting this article I hope you have AWS account and python installed in your machine that’s what we need to do this

To complete this task we need :

  1. ACCESS KEY (which can be found/created using IAM in AWS)
  2. SECRET KEY

import os

import boto3

from botocore.exceptions import ClientError

ACCESS_KEY = ‘your_access_key’

SECRET_KEY = ‘your_secret_key’

bucket_name = ‘testingkit’

client_s3 = boto3.client(

‘s3’,

aws_access_key_id=ACCESS_KEY,

aws_secret_access_key=SECRET_KEY)

file_path = os.path.join(os.getcwd(),)

for file in os.listdir(file_path):

if not file.startswith(‘~’):

try:

client_s3.upload_file(

os.path.join(file_path, file),bucket_name,file

)

except ClientError as CE:

print(‘invalid credentials ‘ + CE)

except Exception as e:

print(e)

In this above code use your ACCESS KEY & SECRET KEY and it will upload your all the files from your present working directory to s3 Bucket to import files from machine I’ve Imported OS module,

and BOTO3 which is a AWS SDK which will help us to connect to s3 and upload our files from machine to Bucket, that’s what we want to do.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ritik

Ritik

Software Engineer, Laravel || php, Python || AWS