import boto3
from botocore.exceptions import ClientError
def generate_s3_presigned_put_url(bucket_name, object_name, expiration=600):
"""
Generates a pre-signed PUT URL to upload a file directly to S3.
:param bucket_name: Target S3 bucket name
:param object_name: Desired object key inside S3
:param expiration: URL validity in seconds (default is 10 minutes)
"""
# Initialize the S3 client (inherits permissions from IAM role)
s3_client = boto3.client('s3', region_name='us-east-1')
try:
response = s3_client.generate_presigned_url(
ClientMethod='put_object',
Params={
'Bucket': bucket_name,
'Key': object_name,
'ContentType': 'application/octet-stream' # Restricts format
},
ExpiresIn=expiration
)
except ClientError as e:
print(f"Error generating pre-signed URL: {e}")
return None
return response
Loading article