Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 presigned link just adding the life span 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 plain url, you know, without the awsaccesskeyid and expires parameters,

Here's 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:

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

What's wrong?

like image 932
Lauro182 Avatar asked May 29 '15 01:05

Lauro182


People also ask

How do I get pre-signed URL S3?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object that you want a presigned URL for. In the Objects list, select the object that you want to create a presigned URL for.

What is pre-signed URL in S3?

A user who does not have AWS credentials or permission to access an S3 object can be granted temporary access by using a presigned URL. A presigned URL is generated by an AWS user who has access to the object. The generated URL is then given to the unauthorized user.

How do I create a signed URL?

A signed URL will be produced for each provided URL, authorized for the specified HTTP method and valid for the given duration. The signurl command uses the private key for a service account (the '<private-key-file>' argument) to generate the cryptographic signature for the generated URL.

Does PHP work in S3?

S3 doesn't run any sort of CGI script (PHP, Perl, Ruby, etc). Think of it as a static html and image repository. If you want to host your PHP application on AWS, consider using AWS Beanstalk. It will launch an environment (server, IP, etc) where you can deploy and run your PHP application easily.


2 Answers

Well, if anyone else has any trouble with this like I did, here is the answer, I went into the amazon php development forums and got help from the profesionals.

It seems you may be flip-flopping between Version 2 and Version 3 of the SDK or looking at the wrong document. Make sure you are getting the one you intend to use and are looking at 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 - API Docs: http://docs.aws.amazon.com/aws-sdk-php/v3/api/index.html - Pre-signed URL Docs: 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 URL Docs: http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#creating-a-pre-signed-url

Mini step-by-step guide of what you have to do:

1.Install composer, preferably using sudo:

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

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

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

}

3.Then go to your project folder in the terminal, and install sdk via composer and update afterward like: (if you change version you have to update again.)

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

4.Then everything is ready for you to follow proper version documentation, in my case for version "aws/aws-sdk-php": "~3.0" and for presigned url, what worked was:

    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 facing the same problems as I did.

like image 151
Lauro182 Avatar answered Oct 21 '22 15:10

Lauro182


If you're using Laravel you can easily get a temporary URL for S3, as documented here: https://laravel.com/docs/master/filesystem#file-urls

Example of your Image model would be:

class Image extends Model
{

    ...

    public function getTemporaryUrlAttribute()
    {
        return Storage::disk('s3')->temporaryUrl(
            $this->path,
            now()->addSeconds(10)
        );
    }
}

Then in your HTML it would be:

<img width="100px" src="{{$image->temporaryUrl}}">
like image 1
Max Avatar answered Oct 21 '22 15:10

Max