python - ImageField 与 django-storages 导致 : "Error parsing the X-Amz-Credential parameter; the region ' us-east- 1' is wrong; expecting ' us-west- 1'"

标签 python django amazon-web-services amazon-s3

我正在尝试将 ImageField 添加到名为 LucyGuide 的模型中,该模型将使用 django-storages 将用户上传的图像保存到 S3 。这是(简化的)模型:

class LucyGuide(TimeStampedModel):
    headshot = models.ImageField(upload_to='uploads/', null=True)
    bio = models.TextField(blank=True)

我已将以下内容添加到我的 settings.py 中:

# Use Boto3 backend to interact with Amazon's S3
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

# Amazon S3 credentials (for django-storages)
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID', default='')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY', default='')

AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME', default='')
AWS_S3_REGION_NAME = os.getenv('AWS_S3_REGION_NAME')
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
# Use v4 of the signing protocol (recommended for all new projects)
AWS_S3_SIGNATURE_VERSION = 's3v4'

其中实际 key 是从 .env 文件读取的(使用类似于 django-de Couple 的机制)。

为了尝试这个,我在 Django 的管理 UI 中为 LucyGuide 上传了一张随机图片:

enter image description here

在 shell 中,我可以访问指南的 headshot 字段的 url 属性,该属性确实是指向 AWS 存储桶的链接:

In [6]: guide = LucyGuide.objects.filter(bio__startswith="Kristen").first()

In [7]: guide
Out[7]: <LucyGuide: Kristen Hoover>

In [8]: guide.headshot
Out[8]: <ImageFieldFile: uploads/320px-Waaah.jpg>

In [9]: guide.headshot.url
Out[9]: 'https://lucy-prod.s3.amazonaws.com/uploads/320px-Waaah.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIMC2A%2F20180327%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180327T200248Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=ae75dbdd75d13020113c12ef2d655e3'

(我删除了部分 URL)。问题是,当我尝试在浏览器中访问此 URL 时,收到“此 XML 文件似乎没有任何与之关联的样式信息”错误:

<Error>
<Code>AuthorizationQueryParametersError</Code>
<Message>
Error parsing the X-Amz-Credential parameter; the region 'us-east-1' is wrong; expecting 'us-west-1'
</Message>
<Region>us-west-1</Region>
<RequestId>1E053D94011E400F</RequestId>
<HostId>
jbkRHVj2y6ygppTsAo2+uOXgby0ok0mbsFsRogKqbu9jPMb+9eGe24nJv441vip3WpmwpFqlsYg=
</HostId>
</Error>

enter image description here

我已经尝试通过添加 AWS_S3_REGION_NAME 设置来解决此问题(参见 http://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html )。错误消息指示的区域 us-west-1 似乎是正确的区域,因为存储桶设置在“美国西部(加利福尼亚北部)”:

enter image description here

总而言之,尽管设置了正确的 AWS_S3_REGION_NAME,但我不明白为什么会发生此错误。我该如何修复这个错误?

更新

如果我检查存储桶中的对象,我会发现它有一个“链接”,它比 headshoturl 属性生成的链接简单得多字段:

enter image description here

我正在考虑将此处显示的“基本 URL”硬编码到我正在尝试构建的用于检索图像 URL 的 API 端点中,但这似乎不是一个优雅的解决方案。还有更好的想法吗?

最佳答案

您无需关闭查询字符串,只需使用 AWS_S3_REGION_NAME = 'us-east-2' # 将 us-east-2 替换为您的区域即可。这样更安全。

关于python - ImageField 与 django-storages 导致 : "Error parsing the X-Amz-Credential parameter; the region ' us-east- 1' is wrong; expecting ' us-west- 1'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49522014/

相关文章:

python - 将 XML 目录与 Python 的 lxml 一起使用?

amazon-web-services - 将 AWS/Route53 子域路由到 ec2 实例

java - 如何拆分 Apache Camel AWS-S3 消息

Django 固定装置和 auto_now_add 日期时间字段

javascript - javascript 中的变量不会传递到表单

python - Django/Python 中的 SQL LIKE

php - 在 Beanstalk 中为 php/symfony 站点配置 apache MPM

python - 导入错误: cannot import name iocpsupport

python - 如何在 Django 中使用动态外键?

python - 如何在python中热重载grpc-server?