AWS SDK for PHP バージョン 3 で署名された Amazon S3 POSTs - AWS SDK for PHP

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK for PHP バージョン 3 で署名された Amazon S3 POSTs

署名付き と同様にURLs、署名付き POSTs を使用すると、 AWS 認証情報を渡さずにユーザーに書き込みアクセスを付与できます。署名付きPOSTフォームは、AwsS3PostObjectV4 のインスタンスを使用して作成できます。

以下の例では、次の方法を示しています。

  • PostObjectV4 を使用して S3 オブジェクトPOSTアップロードフォームのデータを取得します。

のすべてのサンプルコード AWS SDK for PHP は、 で GitHub入手できます。

認証情報

注記

PostObjectV4 は、 AWS IAM Identity Centerからの認証情報では機能しません。

サンプルコードを実行する前に、「」の説明に従って AWS 認証情報を設定します認証情報。次に AWS SDK for PHP、「」で説明されているように、 をインポートします基本的な使用法

Create PostObjectV4

PostObjectV4 のインスタンスを作成するには、次を提供する必要があります。

  • Aws\S3\S3Client のインスタンス

  • bucket (バケット)

  • フォーム入力フィールドの連想配列

  • ポリシー条件の配列 (「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>