Reputation: 1523
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...
Upvotes: 41
Views: 50488
Reputation: 1
For Laravel 9 example to show all images from a protected folder:
$function = function ($image){
return Storage::disk('s3')->temporaryUrl(
$image, Carbon::now()->addMinutes(60)
);
};
$images = Storage::disk('s3')->allFiles('images/client');
return array_map($function,$images);
Upvotes: 0
Reputation: 55
$file_path = "profile/image.png";
$client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();
$bucket = config('filesystems.disks.s3.bucket');
$command = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $file_path // file name in s3 bucket which you want to access
]);
$request = $client->createPresignedRequest($command, '+5 minutes');
return $presignedUrl = (string) $request->getUri(); // Get the actual presigned-url
Upvotes: 0
Reputation: 5731
Here is a full code to upload image to S3 and create signed URL;
First, get a file object and create object name
$filename = $_FILES["image"]["name"];
$array = explode('.', $filename);
$fileExt = $array[1]; // to get file extension
$objectName = 'images/' . time() .$fileExt; // create object name
$document = fopen($_FILES["image"]["tmp_name"],'r');
// Code to upload document on s3
$s3 = \Storage::disk('s3');
$s3->put($objectName,$document,'public');
// If you want to get uploaded document Url you can use below code:
$image_name = \Storage::disk('s3')->url($name); // it will return stored document url
Code to create/get signed URL, you can put it as a saperate function also:
$s3 = \Storage::disk('s3');
$client = $s3->getDriver()->getAdapter()->getClient();
$expiry = "+10 minutes";
$command = $client->getCommand('GetObject', [
'Bucket' => config('params.YOUR_AWS_BUCKET_NAME'), // bucket name
'Key' => $objectName
]);
$request = $client->createPresignedRequest($command, $expiry);
$url = (string) $request->getUri(); // it will return signed URL
Upvotes: 1
Reputation: 1269
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();
Upvotes: 8
Reputation: 501
the above explain answer (@brian_d) is ok, but it takes too much time to generate presigned url. i wasted almost 4-5 days to overcome that. finally following worked for me. Thanks to @Kenth.
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
$disk = Storage::disk('s3');
$url = $disk->getAwsTemporaryUrl($disk->getDriver()->getAdapter(), $value, Carbon::now()->addMinutes(5), []);
Upvotes: 6
Reputation: 1333
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
Upvotes: 64
Reputation: 11385
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"
Upvotes: 60