Last active
June 11, 2025 07:58
-
-
Save Genzer/cacce0bb0c9fa10d79bb7e9d8d26711f to your computer and use it in GitHub Desktop.
Generate a S3 PreSigned URL for uploading files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # /// script | |
| # requires-python = ">=3.13" | |
| # dependencies = [ | |
| # "boto3", | |
| # ] | |
| # /// | |
| import argparse | |
| import boto3 | |
| import sys | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| prog='s3_upload_presigned_url', | |
| description='Generate a S3 PreSigned URL for uploading files') | |
| parser.add_argument('-r', '--region', default=None) | |
| parser.add_argument('-b', '--bucket', required=True, help='Name of S3 Bucket') | |
| parser.add_argument('-p', '--path', required=True, help='A path in Bucket to upload file(s) to.') | |
| parser.add_argument('-e', '--expires', default=3600, help='Seconds to expires. Default to 3600') | |
| args = parser.parse_args() | |
| s3 = boto3.client('s3', region_name=args.region) | |
| presigned_url = s3.generate_presigned_url( | |
| ClientMethod='put_object', | |
| Params={ 'Bucket': args.bucket, 'Key': args.path }, | |
| ExpiresIn=args.expires, | |
| HttpMethod='PUT') | |
| print(presigned_url) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment