Amazon S3 pre-signed URL with Amazon SDK for PHP Version 3 - Amazon SDK for PHP
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon S3 pre-signed URL with Amazon SDK for PHP Version 3

You can authenticate certain types of requests by passing the required information as query-string parameters instead of using the Authorization HTTP header. This is useful for enabling direct third-party browser access to your private Amazon S3 data, without proxying the request. The idea is to construct a “pre-signed” request and encode it as a URL that another user can use. Additionally, you can limit a pre-signed request by specifying an expiration time.

Create a pre-signed URL for an HTTP GET request

The following code example shows how to create a pre-signed URL for an HTTP GET request by using the SDK for PHP.

<?php require 'vendor/autoload.php'; use Aws\S3\S3Client; $s3Client = new S3Client([ 'region' => 'us-west-2', ]); // Supply a CommandInterface object and an expires parameter to the `createPresignedRequest` method. $request = $s3Client->createPresignedRequest( $s3Client->getCommand('GetObject', [ 'Bucket' => 'amzn-s3-demo-bucket', 'Key' => 'demo-key', ]), '+1 hour' ); // From the resulting RequestInterface object, you can get the URL. $presignedUrl = (string) $request->getUri(); echo $presignedUrl;

The API reference for the createPresignedRequestmethod provides more details.

Someone else can use the $presignedUrl value to retrieve the object within the next hour. When the HTTP GET request is made—using a browser, for example—it appears to the S3 service that the call is coming from the user who created the pres-signed URL.

Create a pre-signed URL for an HTTP PUT request

The following code example shows how to create a pre-signed URL for an HTTP PUT request by using the SDK for PHP.

<?php require 'vendor/autoload.php'; use Aws\S3\S3Client; $s3Client = new S3Client([ 'region' => 'us-west-2', ]); $request = $s3Client->createPresignedRequest( $s3Client->getCommand('PutObject', [ 'Bucket' => 'amzn-s3-demo-bucket', 'Key' => 'demo-key', ]), '+1 hour' ); // From the resulting RequestInterface object, you can get the URL. $presignedUrl = (string) $request->getUri();

Someone else can now use the pre-signed URL in an HTTP PUT request to upload a file:

use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; // ... function uploadWithPresignedUrl($presignedUrl, $filePath, $s3Client): ?Response { // Get the HTTP handler from the S3 client. $handler = $s3Client->getHandlerList()->resolve(); // Create a stream from the file. $fileStream = new Stream(fopen($filePath, 'r')); // Create the request. $request = new Request( 'PUT', $presignedUrl, [ 'Content-Type' => mime_content_type($filePath), 'Content-Length' => filesize($filePath) ], $fileStream ); // Send the request using the handler. try { $promise = $handler($request, []); $response = $promise->wait(); return $response; } catch (Exception $e) { echo "Error uploading file: " . $e->getMessage() . "\n"; return null; } }