Skip to content

Instantly share code, notes, and snippets.

@skeptrunedev
Last active February 22, 2024 00:54
Show Gist options
  • Save skeptrunedev/c53321cc4f033b36f075fe541b27e561 to your computer and use it in GitHub Desktop.
Save skeptrunedev/c53321cc4f033b36f075fe541b27e561 to your computer and use it in GitHub Desktop.
s3-image-upload.py
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