【问题标题】:AWS presigned url query string explainedAWS 预签名 url 查询字符串解释
【发布时间】:2020-04-09 16:28:14
【问题描述】:

我正在尝试了解 PUT 方法如何使用预签名的 url 将文件上传到 s3。

我正在使用 boto3 库为 put 调用生成一个预签名的 url。生成的 url 如下所示:

https://My_bucket.s3.amazonaws.com/?AWSAccessKeyId=<ACCESS_KEY>&Signature=<Signature>&x-amz-security-token=<SEC_TOKEN>&Expires=<Expires>

为了生成v4 签名,我需要KeyIdSecretAccessKey

如果我们查看上面的 url,我们可以看到 KeyIdAWSAccessKeyId 匹配,但没有 SecretAccessKey

我使用具有管理权限的帐户生成了预签名 URL(它还具有对 s3 存储桶的读/写访问权限)。据我了解,任何非特权用户都可以使用链接中的信息将文件上传到s3

有很多文档,但坦率地说,我非常困惑。

如果有人能解释一下,我将不胜感激 1.如何使用签名。 2.secret_access_key在哪里?这是从签名派生的吗? 3. 如何使用签名url中的uri查询参数正确生成v4签名?

当我尝试使用我生成的签名时,我得到一个错误

授权标头格式错误;授权组件
“Credential=SIGNATURE/20191217/ap-south-1/s3/aws4_request”格式不正确。

【问题讨论】:

标签: amazon-web-services amazon-s3 boto3 put pre-signed-url


【解决方案1】:

这里的问题是,存储桶的位置与生成预签名 url 的代码位于不同的区域。

以下代码适用于我。

import boto3
from botocore.client import Config

s3_client = boto3.client('s3', endpoint_url='http://s3.ap-south-1.amazonaws.com', config=boto3.session.Config(signature_version='s3v4'))  

response = None
try: 
  response = s3_client.generate_presigned_url(
    'put_object', 
    Params={
      'Bucket': 'bucket-name-to-presign-south1', 
      'Key': 'car.jpg'}, 
      ExpiresIn=5000) 
  print(response)    
except Exception as e: 
  print("In client error exception code") 
  print(e) 

以下参考资料帮助我找到了正确的方向: https://javiermunhoz.com/blog/2016/02/01/on-s3-endpoints-regions-signatures-and-boto-3.html

【讨论】:

猜你喜欢
  • 2018-02-01
  • 2018-10-10
  • 1970-01-01
  • 1970-01-01
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
相关资源
最近更新 更多