Amazon S3 vorsignierte POSTs mit AWS SDK for PHP Version 3 - AWS SDK for PHP

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Amazon S3 vorsignierte POSTs mit AWS SDK for PHP Version 3

Ähnlich wie bei vorsignierten URLs können Sie mit vorsignierten POSTs einem Benutzer Schreibzugriff gewähren, ohne ihm Anmeldeinformationen zu geben. AWS Vorsignierte POST-Formulare können mit Hilfe einer Instanz von AWSS3 V4 erstellt werden. PostObject

In den nachstehenden Beispielen wird Folgendes veranschaulicht:

Der gesamte Beispielcode für AWS SDK for PHP ist hier verfügbar GitHub.

Anmeldeinformationen

Anmerkung

PostObjectV4funktioniert nicht mit Anmeldeinformationen vonAWS IAM Identity Center.

Bevor Sie den Beispielcode ausführen, konfigurieren Sie Ihre AWS Anmeldeinformationen wie unter beschriebenAnmeldeinformationen. Importieren Sie dann dieAWS SDK for PHP, wie unter beschriebenGrundlegende Verwendung.

Erstellen Sie PostObject V4

Um eine Instance von PostObjectV4 zu erstellen, müssen Sie Folgendes bereitstellen:

  • Instance vonAws\S3\S3Client

  • Bucket

  • assoziatives Array mit Formular-Eingabefeldern

  • eine Reihe von Richtlinienbedingungen (siehe Richtlinienkonstruktion im Amazon Simple Storage Service-Benutzerhandbuch)

  • Zeichenfolge für die Ablaufzeit für die Richtlinie (optional, standardmäßig eine Stunde).

Importe

require '../vendor/autoload.php'; use Aws\S3\PostObjectV4; use Aws\S3\S3Client;

Beispiel-Code

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>