Skip to content

Instantly share code, notes, and snippets.

@pmkay
Last active April 16, 2020 17:29
Show Gist options
  • Save pmkay/90d3775ca90e7ff93888e84247eb3ebb to your computer and use it in GitHub Desktop.
Save pmkay/90d3775ca90e7ff93888e84247eb3ebb to your computer and use it in GitHub Desktop.
Save PreSign Private S3 Files on Laravel
<?php
class CleanerS3Helper
{
public function getCleanImageUri() {
$adapter = Storage::disk('s3')->getDriver()->getAdapter();
$command = $adapter->getClient()->getCommand('GetObject', [
'Bucket' => $adapter->getBucket(),
'Key' => $adapter->getPathPrefix().'/images/asd.png'
]);
$request = $adapter->getClient()->createPresignedRequest($command, '+20 minute');
return (string) $request->getUri();
}
}
<?php
Class S3Helper
{
/**
* @param $path
* @return string
* @throws FileNotFoundException
*/
private function s3Url($path)
{
$disk = Storage::disk('s3');
if ($disk->exists($path)) {
$s3_client = $disk->getDriver()->getAdapter()->getClient();
$command = $s3_client->getCommand(
'GetObject',
[
'Bucket' => env('S3_BUCKET'),
'Key' => $path,
'ResponseContentDisposition' => 'attachment;'
]
);
$request = $s3_client->createPresignedRequest($command, '+5 minutes');
return (string) $request->getUri();
} else {
throw new FileNotFoundException($path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment