Created
November 18, 2019 16:33
-
-
Save MarceloZapatta/be5a0fdf4c9eee4f69313731403f575b to your computer and use it in GitHub Desktop.
Generate a presigned URL for AWS S3 Signature V2 aws/aws-sdk-php v2.8.24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Describes how to gerenate a presigned URL to upload a object to S3 | |
| * aws/aws-sdk-php v2.8.24 | |
| */ | |
| define('AWS_KEY', 'ACCESS_KEY'); | |
| define('AWS_SECRET_KEY', 'SECRET_KEY'); | |
| define('AWS_HOST', 'HOST'); | |
| $bucket = 'presigned'; | |
| // require the AWS SDK for php library | |
| require __DIR__ . '/../../../vendor_extra/aws/aws-autoloader.php'; | |
| // Establish connection with host using S3 Client | |
| $client = \Aws\S3\S3Client::factory([ | |
| 'base_url' => AWS_HOST, | |
| 'scheme' => 'https', | |
| 'region' => '', | |
| 'key' => AWS_KEY, | |
| 'secret' => AWS_SECRET_KEY, | |
| ]); | |
| $comandoS3 = $client->getCommand('PutObject', [ | |
| 'Bucket' => $bucket, | |
| 'Key' => 'img_temp.jpg', // Filename | |
| 'ContentType' => 'image/jpeg', // Content-Type of file which will be uploaded | |
| 'Body' => '', // Must have a empty body | |
| 'PathStyle' => true, // Disable virtual host bucket Style, ex: https://host.com/BUCKET/KEY | |
| 'ContentMD5' => false // Fix issue of SignatureDoesNotMatch | |
| ]); | |
| $signedUrl = $comandoS3->createPresignedUrl('+10 minutes'); // PresignedUrl with expiration time, strtotime entry | |
| echo $signedUrl; | |
| exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment