Skip to content

Instantly share code, notes, and snippets.

@LucasAsafe
Last active November 21, 2023 16:28
Show Gist options
  • Save LucasAsafe/57b05258f4f85663e2c43b54b80a392a to your computer and use it in GitHub Desktop.
Save LucasAsafe/57b05258f4f85663e2c43b54b80a392a to your computer and use it in GitHub Desktop.
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
Copy link
Author

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