doudao2954 2018-01-31 16:02
浏览 136
已采纳

将视频从简单形式上传到AWS S3 Bucket [关闭]

I will be short as possible. Anyone knows how to upload a video from an HTML form directly to AWS S3?

I found a guide but I can't find the right settings to get it work.

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

I'm using Wordpress, so plugins allowed, or I will do that with html and php only.

Thanks.

  • 写回答

1条回答 默认 最新

  • duanguochi6194 2018-01-31 16:53
    关注

    Alright, I've gone through my communications with AWS regarding the guide that you and I both had issues with. In the end, they admitted there was problems with the guide and simply told me to use a different approach rather than addressing the situation in which I could never get the signature to properly calculate.

    The different approach they suggested was to use the AWS SDK to generate the signature and all the form fields automatically. So here is a working version based on this guide:

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

    In essence, you use the AWS SDK method PostObjectV4 using S3 connection. It returns all the form values in two arrays, which you use to populate the form. The filename is not the same as what the user uploaded, which it good in that it obfuscates the file so that they can't access it by going to the public URL, but bad in that you need to inject some AJAX to save the filename prior to the submission.

    <?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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向