Skip to content

Instantly share code, notes, and snippets.

@freshyjmp
Created February 5, 2024 22:51
Show Gist options
  • Save freshyjmp/02a71b187b8401ab0e7929222d896351 to your computer and use it in GitHub Desktop.
Save freshyjmp/02a71b187b8401ab0e7929222d896351 to your computer and use it in GitHub Desktop.
Upload Files to S3 Directory
import os
import boto3
import click
from botocore.exceptions import NoCredentialsError
@click.command()
@click.option('--bucket-name', required=True, help='Name of the S3 bucket.')
@click.option('--local-directory', required=True, type=click.Path(exists=True), help='Path to the local directory to sync.')
@click.option('--s3-directory', required=True, help='S3 directory (prefix) to sync with.')
def sync_s3(bucket_name, local_directory, s3_directory):
"""
Syncs files from a LOCAL_DIRECTORY to an S3_DIRECTORY in the specified BUCKET_NAME.
"""
s3 = boto3.client('s3')
for subdir, dirs, files in os.walk(local_directory):
for file in files:
full_path = os.path.join(subdir, file)
s3_path = os.path.join(s3_directory, file).replace(os.sep, '/')
try:
s3.upload_file(full_path, bucket_name, s3_path)
click.echo(f"File {file} uploaded to {s3_path}")
except NoCredentialsError:
click.echo("Credentials not available")
except Exception as e:
click.echo(f"Failed to upload {file}. Reason: {str(e)}")
if __name__ == '__main__':
sync_s3()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment