程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用POST的基于Amazon AWS S3浏览器的上传-大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用POST的基于Amazon AWS S3浏览器的上传-?

开发过程中遇到使用POST的基于Amazon AWS S3浏览器的上传-的问题如何解决?下面主要结合日常开发的经验,给出你关于使用POST的基于Amazon AWS S3浏览器的上传-的解决方法建议,希望对你解决使用POST的基于Amazon AWS S3浏览器的上传-有所启发或帮助;

找到了解决方案:必须明确配置s3客户端以使用Amazon的新签名v4。发生错误是因为它默认为旧版本,从而导致不匹配。一点点脸药- 当时还不是用boto3文档编写的,尽管亚马逊的人说应该很快。

该方法得到了简化,因为它现在可以精确返回所需的字段:

def s3_upload_creds(Name):
    BUCKET = 'mybucket'
    REGION = 'us-West-1'
    s3 = boto3.clIEnt('s3', region_name=REGION, config=Config(signature_version='s3v4'))
    key = '${filename}'
    return s3.generate_presigned_post(
        Bucket = BUCKET,
        Key = key
    )

这意味着可以轻松生成表格:

<HTML>
  <head>
    <Meta http-equiv="Content-Type" content="text/HTML; charset=UTF-8" />
  </head>
  <body>
    {{ creds }}
      <form action="https://mybucket.s3.amazonaws.com" method="post" enctype="multipart/form-data">
        {% for key, value in creds.fIElds.items() %}
          <input type="hIDden" name="{{ key }}" value="{{ value }}" />
        {% endfor %}
      file:
      <input type="file"   name="file" /> <br />
    <input type="submit" name="submit" value="Upload to Amazon S3" />
  </form>
</HTML>

干杯

解决方法

我正在构建一个包含文件上传功能的Web应用程序。我的目标是启动直接从用户上传到S3存储桶的操作。该策略是预先签名一个POST请求,该请求将作为表单提交。

障碍是一个SignatureDoesnotMatch错误-据我所知,我已经遵照文档说明,并且探索了很多选择,但仍然无法解决。我能够生成预签名的下载链接。

引用:

AWS
POST文档

boto3generate_presigned_post

生成签名的请求:

def s3_upload_creds(name,user):
    s3 = boto3.client('s3')
    key = '${filename}'
    region = 'us-east-1'
    date_short = datetiR_202_11845@e.datetiR_202_11845@e.utcnow().strftime('%Y%m%d')
    date_long = datetiR_202_11845@e.datetiR_202_11845@e.utcnow().strftime('%Y%m%dT000000Z')
    fields = { 
        'acl': 'private','date': date_short,'region': region,'x-amz-algorithm': 'AWS4-HMAC-SHA256','x-amz-date': date_long
    }

    return s3.generate_presigned_post(
        Bucket = 'leasy',Fields = fields,Key = key,Conditions = [
            {'acl': 'private'},{'x-amz-algorithm': 'AWS4-HMAC-SHA256'},{'x-amz-credential': '/'.join(['AKI--snip--',date_short,region,'s3','aws4_request'])},{'x-amz-date': date_long}
        ]
    )

上传表格(fields上面填写):

<html>
  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

  </head>
  <body>
  {{ creds }}
  <form action="{{ credS.Url }}" method="post" enctype="multipart/form-data">
    Key to upload: 
    <input type="input"  name="key" value="${filename}" /><br />
    <input type="input"  name="acl" value="{{ creds.fields.acl }}" />
    <input type="hidden" name="Policy" value="{{ creds.fields.policy }}" />
    <input type="text"   name="X-Amz-Algorithm" value="{{ creds.fields['x-amz-algorithm'] }}" />
    <input type="input"   name="X-Amz-Credential" value="{{ creds.fields.AWSAccessKeyId }}/{{ creds.fields.date }}/us-east-1/s3/aws4_request" />
    <input type="input"   name="X-Amz-Date" value="{{ creds.fields['x-amz-date'] }}" />
    <input type="input" name="X-Amz-Signature" value="{{ creds.fields.signature }}" />
    File: 
    <input type="file"   name="file" /> <br />
    <!-- The elements after this will be ignored -->
    <input type="submit" name="submit" value="Upload to Amazon S3" />
  </form>

</html>

回应的相关部分:

<Error>
<Code>SignatureDoesnotMatch</Code>
<message>
The request signature we calculated does not match the signature you provided. check your key and signing method.
</message>
<AWSAccessKeyId>AKI--snip--</AWSAccessKeyId>
<StringToSign>
eyJjb25kaXRpb25zIjogW3siYWNsIjogInByaXZhdGUifSwgeyJ4LWFtei1hbGdvcml0aG0iOiAiQVdTNC1ITUFDLVNIQTI1NiJ9LCB7IngtYW16LWNyZWRlbnRpYWwiOiAiQUtJQUlDVjRNVlBUUlFHU1lLV1EvMjAxNTEymTgvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LCB7IngtYW16LWRhdGUiOiAiMjAxNTEymThUMDAwMDAwWiJ9LCB7ImJ1Y2tldCI6ICJsZWFzeSJ9LCBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAiIl1dLCAiZXhwaXJhdGlvbiI6ICIymDE1LTEyLTE4VDA1OjewOjU2WiJ9
</StringToSign>
<SignatureProvided>wDOjsBRc0iIW7JNtz/4GHgfvKaU=</SignatureProvided>

StringToSign上述错误的Base64解码:

{u'conditions': [{u'acl': u'private'},{u'x-amz-algorithm': u'AWS4-HMAC-SHA256'},{u'x-amz-credential': u'AKI--snip--/20151218/us-east-1/s3/aws4_request'},{u'x-amz-date': u'20151218T000000Z'},{u'bucket': u'leasy'},[u'starts-with',u'$key',u'']],u'expiration': u'2015-12-18T04:59:32Z'}

大佬总结

以上是大佬教程为你收集整理的使用POST的基于Amazon AWS S3浏览器的上传-全部内容,希望文章能够帮你解决使用POST的基于Amazon AWS S3浏览器的上传-所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。