【问题标题】:Problems uploading files to Amazon s3 bucket from a website on heroku从 heroku 上的网站将文件上传到 Amazon s3 存储桶时出现问题
【发布时间】:2018-10-12 09:33:53
【问题描述】:

我正在将我的 Django 网站迁移到 Heroku。但我不知道如何将 Heroku 与 Amazon s3 存储桶一起使用。以前我习惯将所有上传的文件保存在项目中,但我不能使用 Heroku 来做到这一点。

我一直在关注 Heroku 上的文档:https://devcenter.heroku.com/articles/s3https://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


    【解决方案1】:

    似乎我必须在创建客户端对象时添加“aws_access_key_id”和“aws_secret_access_key”。

    s3 = boto3.client(
            's3',
            region_name='eu-central-1',
            aws_access_key_id='KEY',
            aws_secret_access_key='KEY',
        )
    

    【讨论】:

      猜你喜欢
      • 2020-08-07
      • 2015-05-05
      • 2016-03-18
      • 1970-01-01
      • 2018-04-04
      • 2011-06-09
      • 2015-10-28
      • 2018-04-12
      • 1970-01-01
      相关资源
      最近更新 更多