AWS, S3 Storage and limited Visibility

Posted: 2016-04-27 19:23:05

Storing files on S3 is great. And many times those files are private so I only want to give temporary access to them. Using Laravel Storage I can interact with the file-system quite easily and seamlessly especially between Cloud and Local.

In this case I want to return a file only for 10 minutes

    public function getSignedUrl($filename_and_path, $expires_minutes = '10')
    {
        $client     = Storage::disk('s3')->getDriver()->getAdapter()->getClient();
        $bucket     = env('BUCKET');

        $command = $client->getCommand('GetObject', [
            'Bucket' => $bucket,
            'Key' => $filename_and_path
        ]);

        $request = $client->createPresignedRequest($command, Carbon::now()->addMinutes($expires_minutes));

        return (string) $request->getUri();
    }

That is it!

Thanks to this post for the help.


Tags:

php aws