import csv import io import json from functools import cache import boto3 from botocore.config import Config @cache def get_s3_client(): """Get a cached S3 client.""" return boto3.client("s3", config=Config()) def list_s3_objects(bucket, prefix): """List objects in an S3 bucket under a given prefix.""" client = get_s3_client() paginator = client.get_paginator("list_objects_v2") page_iterator = paginator.paginate(Bucket=bucket, Prefix=prefix) objects = [] for page in page_iterator: if "Contents" in page: for obj in page["Contents"]: objects.append(obj["Key"]) return objects def create_presigned_url(bucket, key): """Generate a presigned URL to share an S3 object.""" client = get_s3_client() return client.generate_presigned_url( "get_object", Params={"Bucket": bucket, "Key": key}, ExpiresIn=3600 ) def write_json_to_s3(data, bucket, key): """Write a dictionary to S3 as a JSON file.""" client = get_s3_client() client.put_object( Bucket=bucket, Key=key, Body=json.dumps(data), ContentType="application/json" ) def write_csv_to_s3(header, rows, bucket, key): """Write headers and rows to S3 as a CSV file.""" client = get_s3_client() output = io.StringIO() writer = csv.writer(output) writer.writerow(header) writer.writerows(rows) client.put_object( Bucket=bucket, Key=key, Body=output.getvalue(), ContentType="text/csv" )