Skip to content

Instantly share code, notes, and snippets.

@shentonfreude
Created November 4, 2015 22:36
Show Gist options
  • Save shentonfreude/7825c19df3934acaab99 to your computer and use it in GitHub Desktop.
Save shentonfreude/7825c19df3934acaab99 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Get file path from sys.argv and upload as a presigned URL
import os
import sys
import boto3
import requests
asset_path = sys.argv[1]
_, asset_fname = os.path.split(asset_path)
bucket_name = 'BUCKET_NAME'
ctype = 'image/jpg'
url_lifetime = '3600'
s3_client = boto3.client('s3')
upload_url = s3_client.generate_presigned_url(
'put_object',
Params={
'Bucket': bucket_name,
'Key': asset_path,
'ContentType': ctype,
},
ExpiresIn=url_lifetime,
)
headers = {"Content-Type": ctype}
data = open(asset_path, 'br')
print('{} {}'.format(os.path.getsize(asset_path), asset_path))
try:
resp = requests.put(upload_url, headers=headers, data=data)
except Exception as e:
print("EXCEPTION: {}".format(e))
@shentonfreude
Copy link
Author

I wrote this to test the limits of presigned S3 URL uploads: it's 5.0GiB, which I only found much later in AWS docs about limits of PUT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment