Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel league/flysystem getting file URL with AWS S3

I am trying to build a file management system in Laravel based on league/flysystem: https://github.com/thephpleague/flysystem

I am using the S3 adapter and I have it working to save the uploaded files using:

$filesystem->write('filename.txt', 'contents');

Now I am stuck on generating the download file URL when using the S3 adapter.

The files are saved correctly in the S3 bucket, I have permissions to access them, I just don't know how to get to the S3 getObjectUrl method through the league/flysystem package.

I have tried:

$contents = $filesystem->read('filename.txt');

but that returns the content of the file.

$contents = $filemanager->listContents();

or

$paths = $filemanager->listPaths();

but they give me the relative paths to my files.

What I need is something like "ht...//[s3-region].amazonaws.com/[bucket]/[dir]/[file]..."

like image 521
Ioana Cucuruzan Avatar asked Aug 15 '14 08:08

Ioana Cucuruzan


People also ask

How do I create a URL for Amazon S3?

To generate a presigned URL using the AWS Management ConsoleSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object that you want a presigned URL for.

How do I get files in laravel?

If you have file object from request then you can simply get by laravel function. $extension = $request->file->extension(); dd($extension); If you have file object from request then you can simply get by laravel function.


2 Answers

I am using Laravel 5.2 and the code below seemed to work fine.

Storage::cloud()->url('filename');
like image 138
i906 Avatar answered Oct 21 '22 17:10

i906


I'm not sure what the correct way of doing this is with Flysystem, but the underlying S3Client object has a method for doing that. You could do $filesystem->getAdapter()->getClient()->getObjectUrl($bucket, $key);. Of course, building the URL is as trivial as you described, so you don't really need a special method to do it.

like image 16
Jeremy Lindblom Avatar answered Oct 21 '22 17:10

Jeremy Lindblom