9951 explained code solutions for 126 technologies


php-awsHow do I generate a signed URL for an AWS S3 bucket using PHP?


Generating a signed URL for an AWS S3 bucket using PHP requires the use of the AWS SDK for PHP. The following code example creates a signed URL for an S3 bucket object:

// Include the AWS SDK for PHP
require 'vendor/autoload.php';

// Instantiate an Amazon S3 client
$s3Client = new Aws\S3\S3Client([
    'region'  => 'us-east-1',
    'version' => 'latest'
]);

// Create a signed URL for the object
$cmd = $s3Client->getCommand('getObject', [
    'Bucket' => 'my-bucket',
    'Key'    => 'my-object'
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
$signedUrl = (string)$request->getUri();

echo $signedUrl;

Output example

https://my-bucket.s3.amazonaws.com/my-object?AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&Expires=1583258320&Signature=D6F3DAF5D4F6D5F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6F6

The code:

  1. Includes the AWS SDK for PHP (require 'vendor/autoload.php')
  2. Instantiates an Amazon S3 client ($s3Client = new Aws\S3\S3Client([ ... ]))
  3. Creates a signed URL for the object ($cmd = $s3Client->getCommand('getObject', [ ... ]))
  4. Generates the pre-signed request ($request = $s3Client->createPresignedRequest($cmd, '+20 minutes'))
  5. Retrieves the signed URL ($signedUrl = (string)$request->getUri())
  6. Prints the signed URL (echo $signedUrl)

Helpful links

Edit this code on GitHub