Skip to content

Instantly share code, notes, and snippets.

Embed
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')
try:
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(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)
else:
print('select file contains no docx or png extension')
@LucasAsafe

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 (https://aws.amazon.com/cli/) and you also need to install boto3 - pip install boto3 (https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html).

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