☁️ AWS S3 with Boto3
Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python.
Mastering this concept will significantly boost your Python data science skills!
💻 Code Example:
import boto3 from botocore.exceptions import ClientError from botocore.config import Config import os, json from pathlib import Path # ── S3 Client setup ─────────────────────────────────────────── s3 = boto3.client( "s3", region_name=os.getenv("AWS_REGION", "ap-south-1"), aws_access_key_id =os.getenv("AWS_ACCESS_KEY_ID"), aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"), config=Config(signature_version="s3v4"), ) BUCKET = "pynfinity-media-bucket" # 1. List buckets def list_buckets(): resp = s3.list_buckets() return [b["Name"] for b in resp["Buckets"]] # 2. Upload file def upload_file(local_path: str, s3_key: str, content_type="application/octet-stream"): try: s3.upload_file( local_path, BUCKET, s3_key, ExtraArgs={"ContentType": content_type, "ACL": "private"}, ) print(f"Uploaded: {local_path} → s3://{BUCKET}/{s3_key}") return True except ClientError as e: print(f"Upload failed: {e.response['Error']['Message']}") return False # 3. Download file def download_file(s3_key: str, local_path: str): try: s3.download_file(BUCKET, s3_key, local_path) print(f"Downloaded: s3://{BUCKET}/{s3_key} → {local_path}") except ClientError as e: print(f"Download failed: {e.response['Error']['Message']}") # 4. Generate presigned URL (share without credentials) def get_presigned_url(s3_key: str, expires_in=3600) -> str: try: url = s3.generate_presigned_url( "get_object", Params={"Bucket": BUCKET, "Key": s3_key}, ExpiresIn=expires_in, ) return url except ClientError as e: return str(e) # 5. Upload JSON object directly (no file needed) def put_json(s3_key: str, data: dict): s3.put_object( Bucket=BUCKET, Key=s3_key, Body=json.dumps(data, indent=2), ContentType="application/json", ) print(f"JSON uploaded to s3://{BUCKET}/{s3_key}") # Demo (requires real AWS credentials) # put_json("metadata/pynfinity.json", {"platform": "pynfinity", "author": "santoshtvk"}) print("boto3 S3 utilities ready — configure AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY")
Keep exploring and happy coding! 💻