Skip to content

Instantly share code, notes, and snippets.

@skeptrunedev
Created February 13, 2024 05:51
Show Gist options
  • Save skeptrunedev/371a5320c12f223f33a7eabc25285a93 to your computer and use it in GitHub Desktop.
Save skeptrunedev/371a5320c12f223f33a7eabc25285a93 to your computer and use it in GitHub Desktop.
upload image directory to s3
import os
import dotenv
import boto3
dotenv.load_dotenv()
s3_endpoint = os.getenv("S3_ENDPOINT")
access_key = os.getenv("S3_ACCESS_KEY")
secret_key = os.getenv("S3_SECRET_KEY")
bucket_name = os.getenv("S3_BUCKET")
region = os.getenv("AWS_REGION")
loadfile_img_dir = os.getenv("LOADFILE_IMG_DIR")
session = boto3.Session(
aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region
)
s3 = session.client("s3", endpoint_url=s3_endpoint)
def upload_files_to_s3(local_folder_path):
"""
Uploads all files in the specified local folder to the given S3 bucket.
"""
try:
for file in os.listdir(local_folder_path):
local_file_path = os.path.join(local_folder_path, file)
if os.path.isfile(local_file_path):
print(f"Uploading {file}...")
s3.upload_file(local_file_path, bucket_name, f"images/{file}")
except Exception as e:
print(f"An error occurred: {e}")
upload_files_to_s3(loadfile_img_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment