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:
- Includes the AWS SDK for PHP (
require 'vendor/autoload.php'
) - Instantiates an Amazon S3 client (
$s3Client = new Aws\S3\S3Client([ ... ])
) - Creates a signed URL for the object (
$cmd = $s3Client->getCommand('getObject', [ ... ])
) - Generates the pre-signed request (
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes')
) - Retrieves the signed URL (
$signedUrl = (string)$request->getUri()
) - Prints the signed URL (
echo $signedUrl
)
Helpful links
More of Php Aws
- How do I use PHP to create a ZIP file on AWS?
- How can I use AWS and Zksync together with PHP?
- How can I use Yum to install PHP on an Amazon EC2 instance?
- How can I use an AWS SQS Worker with PHP?
- How can I use AWS WAF to secure my PHP application?
- How can I use AWS PHP SDK without credentials?
- How do I generate an AWS Signature Version 4 with PHP?
- How do I determine the version of PHP I am running on AWS?
- How can I use the AWS S3 S3Client library with PHP?
- How can I connect to an AWS MySQL database using PHP?
See more codes...