Skip to content

Instantly share code, notes, and snippets.

@loughlincodes
Created January 15, 2021 09:31
Show Gist options
  • Save loughlincodes/4da1718914c1299ec733b89c157223e5 to your computer and use it in GitHub Desktop.
Save loughlincodes/4da1718914c1299ec733b89c157223e5 to your computer and use it in GitHub Desktop.
Private function to get an S3 asset url using a presigned request
/**
* Get the url of an S3 file
* @param [string] $key [naem of the file object on S3 e.g. logs/thisismylog.txt]
* @return [string] url to the file
*/
private function getFileUrl($key) {
$s3 = Storage::disk('s3');
$client = $s3->getDriver()->getAdapter()->getClient();
$bucket = config("filesystems.disks.s3.bucket");
$command = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $key
]);
$request = $client->createPresignedRequest($command, '+20 minutes');
return (string) $request->getUri();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment