我有一个django项目,它使用django-storage而不是s3-boto。
问题是,位于S3上的每个文件都无法缓存,因为url在每次调用时都会更改。
下面是django-storage生成的两个调用:
https://my.s3.amazonaws.com/cache/user_6/profile_pic/profile_profile_picture_thumbnail.jpg?Signature=HlVSayUIJj6dMyk%2F4KBtFlz0uJs%3D&Expires=1364418058&AWSAccessKeyId=[awsaccesskey]
https://my.s3.amazonaws.com/cache/user_6/profile_pic/profile_profile_picture_thumbnail.jpg?Signature=xh2VxKys0pkq7yHpbJmH000wkwg%3D&Expires=1364418110&AWSAccessKeyId=[awsaccesskey]如你所见,签名是不同的。我该怎么做才不会破坏我的浏览器缓存呢?
发布于 2013-03-30 04:06:59
在您的设置中,只需添加以下内容:
AWS_QUERYSTRING_AUTH = False这将确保在没有额外参数的情况下生成文件的URL。您的URL将如下所示:
https://my.s3.amazonaws.com/cache/user_6/profile_pic/profile_profile_picture_thumbnail.jpg发布于 2017-03-21 03:14:17
如果设置为AWS_QUERYSTRING_AUTH = True (这是默认设置),每次我们获取url时,django都会生成一个临时url。
如果您不想生成临时url:
将AWS_QUERYSTRING_AUTH = False添加到您的settings.py
如果您仍然需要临时url:
临时urls的有效期为AWS_QUERYSTRING_EXPIRE秒(默认为3600)。所以,我们可以缓存这个临时的url (只要我们缓存的时间不超过它的有效性)。这样-我们可以为后续的页面请求返回相同的url,允许客户端浏览器从其缓存中获取。
settings.py
# We subclass the default storage engine to add some caching
DEFAULT_FILE_STORAGE = 'project.storage.CachedS3Boto3Storage'project/storage.py
import hashlib
from django.conf import settings
from django.core.cache import cache
from storages.backends.s3boto3 import S3Boto3Storage
class CachedS3Boto3Storage(S3Boto3Storage):
""" adds caching for temporary urls """
def url(self, name):
# Add a prefix to avoid conflicts with any other apps
key = hashlib.md5("CachedS3Boto3Storage_%s" % name).hexdigest()
result = cache.get(key)
if result:
return result
# No cached value exists, follow the usual logic
result = super(CachedS3Boto3Storage, self).url(name)
# Cache the result for 3/4 of the temp_url's lifetime.
try:
timeout = settings.AWS_QUERYSTRING_EXPIRE
except:
timeout = 3600
timeout = int(timeout*.75)
cache.set(key, result, timeout)
return result发布于 2016-07-11 22:24:24
保护一些文件存储
你的大多数媒体上传--比如用户头像--你都希望是公开的。但是,如果您有一些媒体,需要身份验证才能访问它-比方说PDF简历,这只对成员可访问-那么您不希望S3BotoStorage的默认S3访问控制列表为公共读取。这里我们不需要子类,因为我们可以传入一个实例而不是引用一个类。
因此,首先移除设置中所有文件字段保护,并添加缓存控制
AWS_HEADERS = {
'Cache-Control': 'max-age=86400',
}
# By default don't protect s3 urls and handle that in the model
AWS_QUERYSTRING_AUTH = False然后将需要保护的文件字段设置为使用自定义受保护路径
from django.db import models
import storages.backends.s3boto
protected_storage = storages.backends.s3boto.S3BotoStorage(
acl='private',
querystring_auth=True,
querystring_expire=600, # 10 minutes, try to ensure people won't/can't share
)
class Profile(models.Model):
resume = models.FileField(
null=True,
blank=True,
help_text='PDF resume accessible only to members',
storage=protected_storage,
)但是当你在dev中的时候,你也需要使用你的普通存储,而且你通常使用本地存储,所以这就是我个人的做法。
if settings.DEFAULT_FILE_STORAGE == 'django.core.files.storage.FileSystemStorage':
protected_storage = FileSystemStorage()
logger.debug('Using FileSystemStorage for resumes files')
else:
protected_storage = S3BotoStorage(
acl='private',
querystring_auth=True,
querystring_expire=86400, # 24Hrs, expiration try to ensure people won't/can't share after 24Hrs
)
logger.debug('Using protected S3BotoStorage for resumes files')参考:https://tartarus.org/james/diary/2013/07/18/fun-with-django-storage-backends
https://stackoverflow.com/questions/15668443
复制相似问题