Get file's signed URL from amazon s3 using Filesystem Laravel 5.2
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"
For Laravel 5.5 and up, you can now use temporary URLs/s3 presigned URL.
use \Storage;
// Make sure you have s3 as your disk driver
$url = Storage::disk('s3')->temporaryUrl(
'file1.jpg', Carbon::now()->addMinutes(5)
);
This only works for s3 storage driver AFAIK.
https://laravel.com/docs/5.5/filesystem#retrieving-files
After lot of bugs, at last, I found the solution of accessing private content of s3 bucket using below code:-
use Storage;
use Config;
$client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();
$bucket = Config::get('filesystems.disks.s3.bucket');
$command = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => '344772707_360.mp4' // file name in s3 bucket which you want to access
]);
$request = $client->createPresignedRequest($command, '+20 minutes');
// Get the actual presigned-url
echo $presignedUrl = (string)$request->getUri();