使用AWS SDK for PHP版本 3 的 Amazon S3 預簽名帖子 - AWS SDK for PHP

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用AWS SDK for PHP版本 3 的 Amazon S3 預簽名帖子

就像預先簽署的 URL 一樣,預先簽署的 POST 可讓您將寫入存取權授予使用者,而無需提供憑據。AWS您可以在 AWSS PostObject 3 V4 的執行個體的協助下建立預先簽署的 POST 表單。

下列範例示範如何:

  • 使用 PostObjectV4 取得 S3 物件 POST 上傳表單的資料。

所有的範例程式碼都可以AWS SDK for PHP在這裡取得 GitHub。

憑證

注意

PostObjectV4不適用於來源的認證AWS IAM Identity Center。

在執行範例程式碼之前,請依照中的說明設定您的AWS認證憑證。然後匯入AWS SDK for PHP,如中所述基本使用

建立第 PostObject 4 版

要建立 PostObjectV4 執行個體,您必須提供下列項目:

  • Aws\S3\S3Client 執行個體

  • 儲存貯體

  • 表單輸入欄位的關聯陣列

  • 一系列政策條件 (請參閱 Amazon 簡單儲存服務使用者指南中的政策建置)

  • 政策的過期時間字串 (選擇性,預設為一小時)。

匯入

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>