簡體   English   中英

從heroku網站上將文件上傳到Amazon s3存儲桶時出現問題

[英]Problems uploading files to Amazon s3 bucket from a website on heroku

我正在將Django網站移至Heroku。 但是我不知道如何在Amazon s3存儲桶中使用Heroku。 以前,我曾經將所有上載的文件保存在項目中,但是我無法使用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)
    }))

您對如何解決這個問題有任何想法嗎?

似乎在創建客戶端對象時必須添加“ 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',
    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM