本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
亚马逊 S3 已预先签 POSTs 名 适用于 PHP 的 Amazon SDK 版本 3
就像预签名一样 URLs,预签名 POSTs 允许您在不向用户提供凭据的情况下向他们 Amazon 授予写入权限。可以在 AWSS PostObject 3 V4 实例的帮助下创建预签名的 POST 表单。
以下示例演示如何:
-
使用 PostObjectV4 获取 S3 对象 POST 上传表单的数据。
的所有示例代码都可以在此 适用于 PHP 的 Amazon SDK 处找到 GitHub
凭证
注意
PostObjectV4
不能使用来自 Amazon IAM Identity Center的凭证。
在运行示例代码之前,请配置您的 Amazon 证书,如中所述凭证。然后导入 适用于 PHP 的 Amazon SDK,如中所述基本用法。
创建 PostObject V4
要创建 PostObjectV4
的实例,必须提供以下内容:
-
Aws\S3\S3Client
的实例 -
桶
-
表单输入字段的关联数组
-
一系列策略条件(请参阅 Amazon Simple Storage Service 用户指南中的策略构建)
-
策略的过期时间字符串(可选,默认为 1 小时)。
导入
require '../vendor/autoload.php'; use Aws\S3\PostObjectV4; use Aws\S3\S3Client;
示例代码
require '../vendor/autoload.php'; use Aws\S3\PostObjectV4; use Aws\S3\S3Client; $client = new S3Client([ 'profile' => 'default', 'region' => 'us-east-1', ]); $bucket = 'amzn-s3-demo-bucket10'; $starts_with = 'user/eric/'; $client->listBuckets(); // Set 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', $starts_with], ]; // Set an expiration time (optional). $expires = '+2 hours'; $postObject = new PostObjectV4( $client, $bucket, $formInputs, $options, $expires ); // Get attributes for the HTML form, for example, action, method, enctype. $formAttributes = $postObject->getFormAttributes(); // Get attributes for the HTML form values. $formInputs = $postObject->getFormInputs(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>PHP</title> </head> <body> <form action="<?php echo $formAttributes['action'] ?>" method="<?php echo $formAttributes['method'] ?>" enctype="<?php echo $formAttributes['enctype'] ?>"> <label id="key"> <input hidden type="text" name="key" value="<?php echo $starts_with ?><?php echo $formInputs['key'] ?>"/> </label> <h3>$formInputs:</h3> acl: <label id="acl"> <input readonly type="text" name="acl" value="<?php echo $formInputs['acl'] ?>"/> </label><br/> X-Amz-Credential: <label id="credential"> <input readonly type="text" name="X-Amz-Credential" value="<?php echo $formInputs['X-Amz-Credential'] ?>"/> </label><br/> X-Amz-Algorithm: <label id="algorithm"> <input readonly type="text" name="X-Amz-Algorithm" value="<?php echo $formInputs['X-Amz-Algorithm'] ?>"/> </label><br/> X-Amz-Date: <label id="date"> <input readonly type="text" name="X-Amz-Date" value="<?php echo $formInputs['X-Amz-Date'] ?>"/> </label><br/><br/><br/> Policy: <label id="policy"> <input readonly type="text" name="Policy" value="<?php echo $formInputs['Policy'] ?>"/> </label><br/> X-Amz-Signature: <label id="signature"> <input readonly type="text" name="X-Amz-Signature" value="<?php echo $formInputs['X-Amz-Signature'] ?>"/> </label><br/><br/> <h3>Choose file:</h3> <input type="file" name="file"/> <br/><br/> <h3>Upload file:</h3> <input type="submit" name="submit" value="Upload to Amazon S3"/> </form> </body> </html>