简体   繁体   中英

Unable to access uploaded image from aws S3 bucket through Django

I have successfully done the configuration of uploading images to s3 bucket through django settings.py file.

this is my config:

  AWS_ACCESS_KEY_ID = '######################'
  AWS_SECRET_ACCESS_KEY = '###########################'
  AWS_STORAGE_BUCKET_NAME = 'divytrust-image'

  AWS_S3_FILE_OVERWRITE = False
  AWS_DEFAULT_ACL = None
  DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

I am using Nginx+gunicorn on server. I am successfully able to upload image to s3bucket/pics But when I try to view the image from django admin panel, I get error:

  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>1F51EJV41QT73FSV</RequestId>
    <HostId>ZL9W4GqDXDbPluBsX+aC4FvRzVy0CLLjy6mXEPL8U/zAWSFzNi1tAQQixGIhbLgeADS1DV0Mv8c= 
  </HostId>

I recently faced the same issue.

The solution is to specify the S3 signature version to be used ie v4. Default for older SDKs for some regions is v2. You may need to upgrade the SDK first.

From Django S3 Docs:

AWS_S3_SIGNATURE_VERSION

As of boto3 version 1.13.21 the default signature version used for generating presigned urls is still v2. To be able to access your s3 objects in all regions through presigned urls, explicitly set this to s3v4.

Also note that the signature version is not backward compatible, which may cause problem with legacy projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM