我会尽可能短。任何人都知道如何将视频从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。
感谢。
答案 0 :(得分:1)
好吧,我已经完成了与AWS的沟通,了解了你和我都有问题的指南。最后,他们承认指南存在问题,只是告诉我使用不同的方法,而不是解决我永远无法正确计算签名的情况。
他们建议的不同方法是使用AWS开发工具包自动生成签名和所有表单字段。所以这是一个基于本指南的工作版本:
https://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/s3-presigned-post.html
实质上,您使用S3 SDK连接使用AWS SDK方法PostObjectV4
。它返回两个数组中的所有表单值,用于填充表单。文件名与用户上传的文件名不同,它很好地混淆了文件以便他们无法通过访问公共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
}