Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble setting cache-cotrol header for Amazon S3 key using boto
    primarykey
    data
    text
    <p>My Django project uses django_compressor to store JavaScript and CSS files in an S3 bucket via boto via the django-storages package.</p> <p>The django-storages-related config includes</p> <pre><code>if 'AWS_STORAGE_BUCKET_NAME' in os.environ: AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME'] AWS_HEADERS = { 'Cache-Control': 'max-age=100000', 'x-amz-acl': 'public-read', } AWS_QUERYSTRING_AUTH = False # This causes images to be stored in Amazon S3 DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' # This causes CSS and other static files to be served from S3 as well. STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage' STATIC_ROOT = '' STATIC_URL = 'https://{0}.s3.amazonaws.com/'.format(AWS_STORAGE_BUCKET_NAME) # This causes conpressed CSS and JavaScript to also go in S3 COMPRESS_STORAGE = STATICFILES_STORAGE COMPRESS_URL = STATIC_URL </code></pre> <p>This works except that when I visit the objects in the S3 management console I see the equals sign in the <code>Cache-Control</code> header has been changed to <code>%3D</code>, as in <code>max-age%3D100000</code>, and this stops caching from working.</p> <p>I wrote a little script to try to fix this along these lines:</p> <pre><code>max_age = 30000000 cache_control = 'public, max-age={}'.format(max_age) con = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY) bucket = con.get_bucket(settings.AWS_STORAGE_BUCKET_NAME) for key in bucket.list(): key.set_metadata('Cache-Control', cache_control) </code></pre> <p>but this does not change the metadata as displayed in Amazon S3 management console. </p> <p><strong>(Update</strong>. <a href="http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html" rel="nofollow">The documentation for S3 metadata</a> says</p> <blockquote> <p>After you upload the object, you cannot modify object metadata. The only way to modify object metadata is to make copy of the object and set the metadata. For more information, go to PUT Object - Copy in the Amazon Simple Storage Service API Reference. You can use the Amazon S3 management console to update the object metadata but internally it makes an object copy replacing the existing object to set the metadata.</p> </blockquote> <p>so perhaps it is not so surprising that I can’t set the metadata. I assume <code>get_metadata</code> is only used when creating the data in the first place.</p> <p><strong>end update)</strong></p> <p>So my questions are, first, can I configure django-storages so that it creates the <code>cache-control</code> header correctly in the first place, and second, is the metadata set with <code>set_metadata</code> the same as the metadata viewed with S3 management console and if not what is the latter and how do I set it programatically?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload