Tags: PHP
I use this function in my Laravel projects to get the S3 URL for a stored file.
/**
* Generate a URL for s3
* @param $minutes (optional), how many minutes before link expires
* @return string
*/
public function getS3Url($minutes = '60')
{
$client = app('filesystem')->disk('s3')->getAdapter()->getClient();
$cmd = $client->getCommand('GetObject', [
'Bucket' => env('AWS_BUCKET'),
'Key' => $this->filename,
]);
$expiration = '+'.$minutes.' minutes';
$request = $client->createPresignedRequest($cmd, $expiration);
return (string) $request->getUri();
}
©2023, kirillsimin.com