【问题标题】:BOTO3 - generate_presigned_url for `put_object` return `The request signature we calculated does not match the signature you provided`BOTO3 - `put_object` 的 generate_presigned_url 返回`我们计算的请求签名与您提供的签名不匹配`
【发布时间】:2018-10-03 11:12:55
【问题描述】:

我正在尝试创建一个预签名的 url 来帮助一些客户上传文件。 这是我当前正在运行的测试脚本

# Get the service client.
s3 = boto3.client('s3')
boto3.set_stream_logger(name='botocore')

# Generate the URL to get 'key-name' from 'bucket-name'
url = s3.generate_presigned_url(
    ClientMethod='put_object',
    Params={
        'Bucket': s3_bucket_name,
        'Key': test_key,
    }    
)

files = StringIO("asdfasdfasdf")
response = requests.put(url, data=files)

print(str(response.content))

但如果我要添加:

`ACL': 'public-read' 

Params(或在我从服务器收到的put_object documentation 中的信息之后添加一些元数据:

The request signature we calculated does not match the signature you provided. Check your key and signing method.

我还打开了一个关于 BOTO3 的问题: https://github.com/boto/boto3/issues/1722

【问题讨论】:

    标签: python amazon-web-services amazon-s3 boto3


    【解决方案1】:

    这在 github 问题 https://github.com/boto/boto3/issues/934 中有介绍

    要上传对象,您应该使用generate_presigned_post。有几个参数不能嵌入到 url 中,这些参数会通过该方法返回给您。

    【讨论】:

      【解决方案2】:

      在戴尔 ECS 对象存储上使用 S3 时遇到了同样的问题。 S3 协议是部分实现的,因此不支持使用 ECS 的 POST 方法。 解决方法是在 PUT 方法中使用正确的标头,然后在服务器上正确设置对象的 ACL。

          # Get the service client.
          s3 = boto3.client('s3')
          boto3.set_stream_logger(name='botocore')
      
          # Generate the URL to get 'key-name' from 'bucket-name'
          url = s3.generate_presigned_url(
              ClientMethod='put_object',
              Params={
                  'Bucket': s3_bucket_name,
                  'Key': test_key,
                  'ACL': 'public-read'
              }    
          )
      
          files = StringIO("asdfasdfasdf")
          headers = {'x-amz-acl': 'public-read'}
          response = requests.put(url, data=files, headers=headers)
      
          print(str(response.content))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-18
        • 2021-10-15
        • 2014-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多