Skip to content

Instantly share code, notes, and snippets.

@quietcricket
Last active February 23, 2021 02:23
Show Gist options
  • Save quietcricket/361bab715b7eb839dad8c86020f1fb12 to your computer and use it in GitHub Desktop.
Save quietcricket/361bab715b7eb839dad8c86020f1fb12 to your computer and use it in GitHub Desktop.
Upload file to aws s3 with boto3
import mimetypes
def upload_s3(key, fileobj, bucket_name, cache=999999):
"""
Lots of problems with new boto3 library. Many functions are not documented or wrongly documented
1. s3.Object.metadata, s3.Object.cache_control, s3.Object.content_type: are read only, gotcha
2. If you set it with `ExtraArgs={'Metadata':{...}}`, wrong again. Cache-Control will become amz-x-cache-control,
not what you wanted
3. If you set it with `ExtraArgs={'Cache-Control':'max-age=999'}`, wrong again! They need too be CacheControl and ContentType
"""
s3 = boto3.resource('s3')
obj = s3.Object(bucket_name, key)
metadata = {'CacheControl': 'max-age=%i' % cache}
metadata['ContentType'] = mimetypes.guess_type(key)[0]
obj.upload_fileobj(fileobj, ExtraArgs=metadata)
# This is how you allow the object to be read by public
obj.Acl().put(ACL='public-read')
return obj.content_length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment