我一直在尝试设置S3来提供我的静态文件。文件似乎在存储桶中,并且指向文件的链接似乎通过OK进入了我的模板中,但实际上并未应用这些文件。当我查看浏览器的检查器时,会看到类似这样的脚本标签
<script src="https://mybucket.s3.amazonaws.com/static/js/jquery.js?AWSAccessKeyId=REDACTED&Signature=REDACTED&Expires=00000000"></script>
注意:我已将链接中的值更改为REDACTED和00000000
当我刚去
https://mybucket.s3.amazonaws.com/static/js/jquery.js
我实际上可以看到代码。但是当我去
https://mybucket.s3.amazonaws.com/static/js/jquery.js?AWSAccessKeyId=REDACTED&Signature=REDACTED&Expires=00000000
我收到以下消息
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>InvalidRequest</Code>
<Message>
The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.
</Message>
<RequestId>REDACTED</RequestId>
<HostId>
REDACTED
</HostId>
</Error>
编辑:根据Micheal J Roberts的建议,问题似乎与签名有关。 django-storages码头表示您需要添加
S3_USE_SIGV4 = True
到settings.py。我已经做到了,但这似乎无法解决我的问题。也许我没有正确执行此操作,或者其他一些设置正在干扰我?
我在与设置相同的文件夹中创建了一个文件夹 aws ,其中有一个名为utils.py
的文件from storages.backends.s3boto3 import S3Boto3Storage
StaticRootS3BotoStorage = lambda: S3Boto3Storage(location='static')
MediaRootS3BotoStorage = lambda: S3Boto3Storage(location='media')
然后在我的settings.py中输入
import datetime
AWS_ACCESS_KEY_ID = 'REDACTED'
AWS_SECRET_ACCESS_KEY = 'REDACTED'
AWS_FILE_EXPIRE = 200
S3_USE_SIGV4 = True
AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = True
DEFAULT_FILE_STORAGE = 'MyProject.aws.utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'MyProject.aws.utils.StaticRootS3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'mybucket'
S3DIRECT_REGION = 'eu-central-1'
AWS_S3_HOST = 's3.eu-central-1.amazonaws.com'
S3_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = '//%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
STATIC_URL = S3_URL + 'static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
two_months = datetime.timedelta(days=61)
date_two_months_later = datetime.date.today() + two_months
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 GMT")
AWS_HEADERS = {
'Expires': expires,
'Cache-Control': 'max-age=%d' % (int(two_months.total_seconds()), ),
}
这里有什么问题吗?
这与我尚未为SSL配置项目有关吗?还是这真的是AWS的事情。
在加载页面时,在我的日志中显示“使用hmacv1 auth计算签名”。这个版本应该是v4吗?还是这并不表示什么?