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

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试