Creating amazon aws s3 pre signed url php

According to this link http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html , I can easily create a designated connection by simply adding a lifetime to getObjectUrl

$signedUrl = $client->getObjectUrl($bucket, 'data.txt', '+10 minutes'); // > https://my-bucket.s3.amazonaws.com/data.txt?AWSAccessKeyId=[...]&Expires=[...]&Signature=[...] 

But I get a simple url, you know, without awsaccesskeyid and end parameters,

Here is my code:

 $bucket = 'imagenesfc'; $keyname = 'NASimagenes/codigoBarraBoleto/1001000098.png'; $filepath = 'NASimagenes/codigoBarraBoleto'; // Instantiate the client. $s3 = S3Client::factory(array( 'version' => 'latest', 'region' => 'us-west-1' )); $signedUrl = $s3->getObjectUrl($bucket, $keyname,'+10 minutes'); // > https://my-bucket.s3.amazonaws.com/data.txt?AWSAccessKeyId=[...]&Expires=[...]&Signature=[...] echo $signedUrl."<br>"; 

EDIT: I have AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables

My echo looks like this:

https://s3-us-west-1.amazonaws.com/imagenesfc/NASimagenes/codigoBarraBoleto/1001000098.png

What's wrong?

+7
php amazon-s3 pre-signed-url
source share
1 answer

Well, if someone else has problems with this, as I, here is the answer, I went to php Amazon development forums and got help from professionals.

It seems like you might flip between version 2 and version 3 of the SDK or look at the wrong document. Make sure you get the one you intend to use and see the correct documentation. They are different.

V3 - Composer requirement: {"aws / aws-sdk-php": "~ 3.0"} - User Guide: http://docs.aws.amazon.com/aws-sdk-php/v3/guide/index.html - Docs API: http://docs.aws.amazon.com/aws-sdk-php/v3/api/index.html - Pre-signed URLs: http://docs.aws.amazon.com/aws- sdk-php / v3 / guide / service / s3-presigned-url.html

V2 - Composer requirement: {"aws / aws-sdk-php": "~ 2.8"} - User Guide: http://docs.aws.amazon.com/aws-sdk-php/v2/guide/index.html - API Docs: http://docs.aws.amazon.com/aws-sdk-php/v2/api/index.html - Pre-signed documents URL: http://docs.aws.amazon.com/aws-sdk -php / v2 / guide / service-s3.html # creating-a-pre-signed-url

Thumbnail walkthrough you should do:

1.Install the composer, preferably using sudo:

  sudo curl -sS https://getcomposer.org/installer | sudo php 

2. Go to the project folder and create the composer.json file, with the version you need, you can find here: https://github.com/aws/aws-sdk-php/releases , the commands for each version seem very specific to the version be careful, this was my main problem.

 { "require": { "aws/aws-sdk-php": "~3.0" } 

}

3. Then go to the project folder on the terminal and install sdk through the composer and update it as follows: (if you change the version, you need to update it again.)

  sudo php composer.phar install sudo php composer.phar update 

4. Then everything is ready for you to follow the correct version documentation, in my case for the version "aws / aws-sdk-php": "~ 3.0" and for the assigned URL it worked:

  require 'vendor/autoload.php'; use Aws\S3\S3Client; use Aws\S3\Exception\S3Exception; $sharedConfig = [ 'region' => 'us-west-1', 'version' => 'latest' ]; //I have AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables $s3Client = new Aws\S3\S3Client($sharedConfig); $cmd = $s3Client->getCommand('GetObject', [ 'Bucket' => $bucket, 'Key' => $keyname ]); $request = $s3Client->createPresignedRequest($cmd, '+20 minutes'); $presignedUrl = (string) $request->getUri(); echo $presignedUrl; 

I hope this helps anyone who is facing the same problems as me.

+17
source share

All Articles