Last active
April 16, 2020 17:29
-
-
Save pmkay/90d3775ca90e7ff93888e84247eb3ebb to your computer and use it in GitHub Desktop.
Save PreSign Private S3 Files on Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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