【问题标题】:Browser Based File Upload on AWS S3 using POST Request使用 POST 请求在 AWS S3 上上传基于浏览器的文件
【发布时间】:2018-02-25 03:26:52
【问题描述】:

这是我的 HTML POST 表单。

<html>
  <head>

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

  </head>
  <body>

  <form action="http://sigv4examplebucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
    Key to upload: 
    <input type="input"  name="key" value="user/user1/${filename}" /><br />
    <input type="hidden" name="acl" value="public-read" />
    <input type="hidden" name="success_action_redirect" value="http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html" />
    Content-Type: 
    <input type="input"  name="Content-Type" value="image/jpeg" /><br />
    <input type="hidden" name="x-amz-meta-uuid" value="14365123651274" /> 
    <input type="hidden" name="x-amz-server-side-encryption" value="AES256" /> 
    <input type="text"   name="X-Amz-Credential" value="AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request" />
    <input type="text"   name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
    <input type="text"   name="X-Amz-Date" value="20151229T000000Z" />

    Tags for File: 
    <input type="input"  name="x-amz-meta-tag" value="" /><br />
    <input type="hidden" name="Policy" value='<Base64-encoded policy string>' />
    <input type="hidden" name="X-Amz-Signature" value="<signature-value>" />
    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>

我从下面显示的 AWS S3 文档中得到了这个。

https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html

从 AWS 控制台我获得了如下所示的安全凭证。

我知道我需要为“Policy”和“X-Amz-Signature”设置值,但我不知道该怎么做。

在他们提到的文档中,我需要 StringToSign 并获取策略/签名,但我不知道该怎么做。

有人可以帮助我了解如何为我的 HTML 表单生成策略和签名吗?

【问题讨论】:

    标签: amazon-web-services amazon-s3


    【解决方案1】:

    您必须从后端计算签名。按照这些详细信息Calculating a Signature 自行实施。

    类似于this:

    $kDate = hash_hmac('sha256', $short_date, 'AWS4' . $secret_key, true);
    $kRegion = hash_hmac('sha256', $region, $kDate, true);
    $kService = hash_hmac('sha256', "s3", $kRegion, true);
    $kSigning = hash_hmac('sha256', "aws4_request", $kService, true);
    $signature = hash_hmac('sha256', base64_encode($policy), $kSigning);
    

    或者您可以使用您选择的任何AWS SDKs

    例如使用PHP SDK,您将实现:

    Aws\Signature\S3SignatureV4
    

    【讨论】:

    • 能否提供 GoLang 的示例?
    猜你喜欢
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 2012-01-23
    • 1970-01-01
    相关资源
    最近更新 更多