本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Amazon S3 预签名 POSTAmazon SDK for PHP版本 3
与预签名 URL 极其相似,预签名 POST 使您无需向用户授予访问权限Amazon凭证。可以借助实例创建预签名 POST 表单awss3PostObjectV4.
以下示例演示如何:
-
使用获取 S3 Object POST 上传的数据PostObjectV4.
的所有示例代码Amazon SDK for PHP可用此处 GitHub
凭证
运行示例代码之前,请配置您的Amazon凭证,如所述设置凭证. 然后导入Amazon SDK for PHP,如所述基本用法.
Create PostObjectV4
要创建 PostObjectV4
的实例,必须提供以下内容:
-
Aws\S3\S3Client
的实例 -
桶
-
表单输入字段的关联数组
-
策略条件数组 (请参见策略构建((Amazon Simple Storage Service
-
策略的过期时间字符串(可选,默认为 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();