使用 Amazon SDK for PHP 版本 3 的 Amazon S3 预签名 POST - Amazon SDK for PHP
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon SDK for PHP 版本 3 的 Amazon S3 预签名 POST

与预签名 URL 极其相似,预签名 POST 使您无需向用户提供 Amazon 凭证便可向其授予写入访问权限。可以在 AwsS3PostObjectV4 实例的帮助下创建预签名 POST 表单。

以下示例演示如何:

  • 使用 PostObjectV4 获取 S3 Object POST 上传的数据。

Amazon SDK for PHPGitHub 上提供了的所有示例代码。

凭证

注意

PostObjectV4 不能使用来自 Amazon IAM Identity Center 的凭证。

运行示例代码之前,请配置您的 Amazon 凭证,如 凭证 中所述。然后导入 Amazon SDK for PHP,如 基本用法 中所述。

创建 PostObjectV4

要创建 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 = 'doc-example-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>