Categories
python

upload directory to Amazon S3


import os
import sys
import boto3

def upload_folder_to_s3(local_folder_path, s3_bucket_name):
    s3_client = boto3.client('s3')
    for root, dirs, files in os.walk(local_folder_path):
        for file in files:
            local_file_path = os.path.join(root, file)
            s3_key = os.path.relpath(local_file_path, local_folder_path)
            s3_client.upload_file(local_file_path, s3_bucket_name, s3_key)
            print(f"Uploaded '{local_file_path}' to '{s3_bucket_name}/{s3_key}'")

if name == 'main':
    if len(sys.argv) < 3:
        print("Usage: python3 script.py   ")
        sys.exit(1)

    s3_key = sys.argv[1]
    local_folder_path = sys.argv[2]
    s3_bucket_name = sys.argv[3]

    upload_folder_to_s3(local_folder_path, s3_bucket_name)

Leave a Reply

Your email address will not be published. Required fields are marked *