Skip to content

Instantly share code, notes, and snippets.

@shentonfreude
Created January 23, 2017 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shentonfreude/36d0910984cefd42ab503dba61ada049 to your computer and use it in GitHub Desktop.
Save shentonfreude/36d0910984cefd42ab503dba61ada049 to your computer and use it in GitHub Desktop.
Setting CacheControl in S3 copy requires MetadataDirective=REPLACE .... or not, depending on size
#!/usr/bin/env python
#
# If you do not set MetadataDirective=REPLACE the managed copy will NOT set the
# CacheControl on the copy, nor will it warn you your attributes are being
# ignored. That is, unless the source is large enough to trigger the
# MultipartUpload, which will in fact set the CacheControl. This behavior
# difference based on size is -- to say the least -- surprising.
# See kyleknap's comment: https://github.com/aws/aws-cli/pull/1188
#
# Note that when using REPLACE (on "small" files) it will replace all metadata
# on the destination, including things the original likely would have, such as
# Content-Type; make sure you set all destination attributes explicitely.
from datetime import datetime
import boto3
s3client = boto3.client('s3')
bucket = 'mybucket'
src_key = '000_Test5G.mov'
dst_key = '000_Test5G' + datetime.utcnow().isoformat() + '.mov'
# logging.basicConfig(level=logging.DEBUG)
s3client.copy(
CopySource={'Bucket': bucket, 'Key': src_key},
Bucket=bucket,
Key=dst_key,
ExtraArgs={'CacheControl': 'public, max-age=300, s-max-age=600',
'MetadataDirective': 'REPLACE',
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment