【发布时间】:2018-10-12 09:33:53
【问题描述】:
我正在将我的 Django 网站迁移到 Heroku。但我不知道如何将 Heroku 与 Amazon s3 存储桶一起使用。以前我习惯将所有上传的文件保存在项目中,但我不能使用 Heroku 来做到这一点。
我一直在关注 Heroku 上的文档:https://devcenter.heroku.com/articles/s3 和 https://devcenter.heroku.com/articles/s3-upload-python 该存储桶位于法兰克福。
上传文件时出现以下错误:
<Error>
<Code>InvalidArgument</Code>
<Message>a non-empty Access Key (AKID) must be provided in the credential.</Message>
<ArgumentName>X-Amz-Credential</ArgumentName>
<ArgumentValue>/20180502/eu-central-1/s3/aws4_request</ArgumentValue>
…
</Error>
我在 Heroku 中指定了这样的键:“heroku config:set AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=yyy”
你可以去https://eventcollective.herokuapp.com/accounts/account/尝试上传图片。
来自views.py
def sign_s3(request):
S3_BUCKET = os.environ.get('S3_BUCKET')
print(S3_BUCKET)
file_name = request.GET.get('file_name')
file_type = request.GET.get('file_type')
s3 = boto3.client('s3', region_name='eu-central-1')
presigned_post = s3.generate_presigned_post(
Bucket=S3_BUCKET,
Key=file_name,
Fields={"acl": "public-read", "Content-Type": file_type},
Conditions=[
{"acl": "public-read"},
{"Content-Type": file_type}
],
ExpiresIn=3600
)
return HttpResponse(json.dumps({
'data': presigned_post,
'url': 'https://%s.s3.amazonaws.com/%s' % (S3_BUCKET, file_name)
}))
你知道如何解决这个问题吗?
【问题讨论】:
标签: python django heroku amazon-s3