Presigned URL for AWS S3 Object with PHP

This is a tutorial on how to generate a presigned url for Amazon AWS S3 Object with PHP.

This is a short tutorial on how to create a presigned url for an amazon S3 object.

require('./aws/aws-autoloader.php');

use Aws\S3\S3Client;

$bucket = 'mybucketname';

$s3 = S3Client::factory(array(
    'region'  => 'us-west-2',
    'version' => 'latest',
    'credentials' => [
        'key'    => 'AWS_KEY',
        'secret' => 'AWS_SECRET'
    ]
));

$cmd = $s3->getCommand('GetObject', [
    'Bucket' => $bucket,
    'Key'    => $file_key
]);

$request = $s3->createPresignedRequest($cmd, '+20 minutes');

// Get the actual presigned-url
$presignedUrl = (string) $request->getUri();

 

Full Documentation: https://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/s3-presigned-url.html