AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Amazon S3 预签名 POSTs 与 AWS SDK for PHP 版本 3
与预签名 URLs 极其相似,预签名 POSTs 使您无需向用户提供 AWS 凭证便可向其授予写入访问权限。可在 AwsS3PostObjectV4 实例的帮助下创建预签名 POST 表单。
以下示例演示如何:
版本 3 的所有示例代码在 AWS SDK for PHP 上的此处提供。GitHub
Credentials
创建PostObjectV4
要创建 PostObjectV4 的实例,必须提供以下内容:
Aws\S3\S3Client 的实例
存储桶
表单输入字段的关联数组
策略条件数组(请参见 Amazon S3 Developer Guide 中的策略构造)
策略的过期时间字符串(可选,默认为 1 小时)。
导入
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
示例代码
$client = new S3Client([
'profile' => 'default',
'version' => 'latest',
'region' => 'us-west-2',
]);
$bucket = 'mybucket';
// 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', 'user/eric/'],
];
// 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();