【问题标题】:upload video from simple form to AWS S3 Bucket [closed]将视频从简单表单上传到 AWS S3 Bucket [关闭]
【发布时间】:2018-07-10 19:32:25
【问题描述】:

我会尽量矮。任何人都知道如何将 HTML 表单中的视频直接上传到 AWS S3?

我找到了一个指南,但我找不到正确的设置来让它工作。

<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>

我使用的是 Wordpress,所以允许使用插件,否则我将只使用 html 和 php。

谢谢。

【问题讨论】:

  • 你之前没有发过类似的东西吗? stackoverflow.com/questions/48504704/… - 看起来像是对我的转发。
  • 你不应该转发。您应该编辑您的其他问题并投票自己重新打开它。
  • 您确定您的凭证被正确编码了吗?我遇到了并非如此的问题,这导致上传表单无法正常工作。
  • 另外,我确实遇到了一些问题,因为某些 AWS 文档要么过时、不正确,要么使用了不同版本的 API。
  • 我将提供一个答案,我遇到了类似的问题,不得不致电支持。发现一些问题花了几个小时,我解释说他们的示例代码也是错误的。他们说哈希应该不使用“\r”字符来生成,但他们的示例哈希只能使用“\r”字符来计算。

标签: php wordpress forms upload


【解决方案1】:

好的,我已经与 AWS 就您和我都遇到过的指南进行了沟通。最后,他们承认指南存在问题,只是告诉我使用不同的方法,而不是解决我永远无法正确计算签名的情况。

他们建议的不同方法是使用 AWS 开发工具包自动生成签名和所有表单字段。因此,这是基于本指南的工作版本:

https://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/s3-presigned-post.html

本质上,您使用 AWS 开发工具包方法 PostObjectV4 使用 S3 连接。它返回两个数组中的所有表单值,用于填充表单。文件名与用户上传的文件名不同,这很好,因为它混淆了文件,因此他们无法通过公共 URL 访问它,但不好的是你需要注入一些 AJAX 来保存文件名在提交之前。

<?php
require('aws.phar');

try {
 $client = new \Aws\S3\S3Client([
  'version' => 'latest',
  'region' => 'us-east-1',
 ]);
 $bucket = <your_bucket_name>;

 // Set some defaults for form input fields
 $formInputs = ['acl' => 'public-read'];

 // Construct an array of conditions for policy
 $options = [
  ['acl' => 'public-read'],
  ['bucket' => $bucket],
  ['starts-with', '$key', 'test/'],
 ];

 // Optional: configure expiration time string
 $expires = '+2 hours';

 $postObject = new \Aws\S3\PostObjectV4($client,$bucket,$formInputs,$options,$expires);

 // Get attributes to set on an HTML form, e.g., action, method, enctype
 $formAttributes = $postObject->getFormAttributes();

 // Get form input fields. This will include anything set as a form input in
 // the constructor, the provided JSON policy, your AWS Access Key ID, and
 // an auth signature.
 $formInputs = $postObject->getFormInputs();

} catch (Exception $e) {
 echo "Error $e";
}

if ($formAttributes && $formInputs) {

 $file_key = 'test/file-'.time();
 $upload_acl = 'public-read';
?>
<html>
  <head>
    <title>S3 POST Form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>

  <body>
    <form id="upload_form" action="<?php echo $formAttributes['action'];?>" method="post" enctype="multipart/form-data">
      <input type="hidden" name="X-Amz-Security-Token" value="<?php echo $formInputs['X-Amz-Security-Token'];?>">
      <input type="hidden" name="acl" value="<?php echo $upload_acl;?>">
      <input type="hidden" name="key" value="<?php echo $file_key;?>">
      <input type="hidden" name="X-Amz-Credential" value="<?php echo $formInputs['X-Amz-Credential'];?>">
      <input type="hidden" name="X-Amz-Algorithm" value="<?php echo $formInputs['X-Amz-Algorithm'];?>">
      <input type="hidden" name="X-Amz-Date" value="<?php echo $formInputs['X-Amz-Date'];?>">
      <input type="hidden" name="policy" value="<?php echo $formInputs['Policy'];?>">
      <input type="hidden" name="X-Amz-Signature" value="<?php echo $formInputs['X-Amz-Signature'];?>">
      File to upload to S3:
      <input id="upload_file" name="file" type="file">
      <br>
      <input type="submit" value="Upload File to S3">
    </form>
  </body>
</html>
<?php
}

【讨论】:

  • 谢谢你的回答,我马上去试试。
  • 好的,我找到了解决方案。我使用了没有 SDK 的旧代码,并且找到了正确的设置“访问密钥”和“策略”。再次感谢,您的回答是一个很好的提示。
猜你喜欢
  • 2016-06-07
  • 2015-11-07
  • 2018-09-30
  • 2014-04-30
  • 2016-01-31
  • 2021-09-21
  • 2015-04-12
  • 2016-08-15
  • 2018-04-19
相关资源
最近更新 更多