故障设置缓存cotrol头亚马逊S3键使用博托亚马逊、缓存、故障、cotrol

2023-09-11 09:29:22 作者:小三控 い

我的Django项目使用django_com pressor通过博托通过Django的存储器封装存储在S3存储桶JavaScript和CSS文件。

My Django project uses django_compressor to store JavaScript and CSS files in an S3 bucket via boto via the django-storages package.

Django的-存储器相关的配置包括:

The django-storages-related config includes

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

该作品只是当我访问的对象在S3管理控制台我看到了等号,在的Cache-Control 标题已改为%3D ,如的max-age%3D100000 ,而这种停止高速缓存无法正常工作。

This works except that when I visit the objects in the S3 management console I see the equals sign in the Cache-Control header has been changed to %3D, as in max-age%3D100000, and this stops caching from working.

我写了一个小脚本来尝试解决这个问题沿着这些路线:

I wrote a little script to try to fix this along these lines:

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)

但这中所显示的Amazon S3管理控制台不改变的元数据。

but this does not change the metadata as displayed in Amazon S3 management console.

(更新。的文档S3元数据说

在您上传的对象,则无法修改的对象元数据。修改对象元数据的唯一方法是使对象的副本,并设置的元数据。欲了解更多信息,请把对象 - 复制在亚马逊简单存储服务API参考。您可以使用Amazon S3管理控制台来更新对象元数据,但在内部它使一个对象的副本替换现有的对象来设置的元数据。

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.

因此​​,也许它不是那么奇怪,我无法设置的元数据。我认为 get_metadata 首先创建数据时才使用。

so perhaps it is not so surprising that I can’t set the metadata. I assume get_metadata is only used when creating the data in the first place.

末更新)

所以我的问题是,第一,我可以配置Django的存储器,以便它正确地摆在首位创建的Cache-Control 头,二是元数据设置 set_metadata 一样与S3管理控制台查看的元数据,如果没有什么是后者,我如何编程设置呢?

So my questions are, first, can I configure django-storages so that it creates the cache-control header correctly in the first place, and second, is the metadata set with set_metadata the same as the metadata viewed with S3 management console and if not what is the latter and how do I set it programatically?

推荐答案

使用ASCII字符串值,解决了这个对我来说。

Use ASCII string as values solves this for me.

AWS_HEADERS = {'Cache-Control': str('public, max-age=15552000')}
 
精彩推荐
图片推荐