Get file's signed URL from amazon s3 using Filesystem Laravel 5.2

Issue

I’m looking for a good solution to get the signed url from amazon s3.

I have a version working with it, but not using laravel:

private function getUrl ()
{
        $distribution = $_SERVER["AWS_CDN_URL"];

        $cf = Amazon::getCFClient();

        $url = $cf->getSignedUrl(array(
            'url'     => $distribution . self::AWS_PATH.rawurlencode($this->fileName),
            'expires' => time() + (session_cache_expire() * 60)));

        return $url;
}

I don’t know if this is the best way to do with laravel, considering it has a entire file system to work…

But if don’t have another way, how do I get the client? Debugging I’ve found an instance of it inside the Filesystem object, but it is protected…

Solution

In Laravel,

$s3 = \Storage::disk('s3');
$client = $s3->getDriver()->getAdapter()->getClient();
$expiry = "+10 minutes";

$command = $client->getCommand('GetObject', [
    'Bucket' => \Config::get('filesystems.disks.s3.bucket'),
    'Key'    => "file/in/s3/bucket"
]);

$request = $client->createPresignedRequest($command, $expiry);

return (string) $request->getUri();

Make sure you have the AWS for flysystem composer package too (version will vary):

"league/flysystem-aws-s3-v3": "1.0.9"

Answered By – brian_d

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published