php-awsHow can I generate a presigned URL using the AWS PHP SDK?
Generating a presigned URL using the AWS PHP SDK is relatively straightforward. The following example code block demonstrates how to generate a presigned URL with the AWS SDK for PHP:
<?php
// Include the AWS SDK using the Composer autoloader
require 'vendor/autoload.php';
// Create an Amazon S3 client object
$s3Client = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => [
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
]
]);
// Set the bucket and object name
$bucket = 'my-bucket';
$keyname = 'my-object';
// Create a pre-signed URL for a request
$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $keyname
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
// Get the pre-signed URL
$presignedUrl = (string) $request->getUri();
echo $presignedUrl;
This example code will output a presigned URL, similar to the following:
https://my-bucket.s3.amazonaws.com/my-object?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=YOUR_AWS_ACCESS_KEY_ID%2F20200708%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20200708T000000Z&X-Amz-Expires=1200&X-Amz-SignedHeaders=host&X-Amz-Signature=f5d2d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7
The example code consists of the following parts:
- Include the AWS SDK using the Composer autoloader.
- Create an Amazon S3 client object.
- Set the bucket and object name.
- Create a pre-signed URL for a request.
- Get the pre-signed URL.
- Output the pre-signed URL.
For more information on generating presigned URLs with the AWS SDK for PHP, see the following documentation:
More of Php Aws
- How can I use PHP to connect to an Amazon Aurora database?
- 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?
See more codes...