Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python code to upload all files, containing defined extension, into AWS S3 using boto3 and os.walker
import os
import logging
import boto3
from botocore.exceptions import ClientError
path = '/home/{your user}/Documents'
def upload_file(file_name, bucket, object_name=None):
if object_name is None:
object_name = file_name
s3_client = boto3.client('s3')
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
return False
return True
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.docx' in file:
upload_file(os.path.join(r, file), 'bucketname', file)
elif '.png' in file:
upload_file(os.path.join(r, file), 'bucketname', file)
print('select file contains no docx or png extension')

This comment has been minimized.

Copy link
Owner Author

@LucasAsafe LucasAsafe commented Jan 13, 2020

To make it work, you need to install and set up AWS CLI - pip install awscli ( and you also need to install boto3 - pip install boto3 (

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment