GrahamCampbell/Laravel-Flysystem

How to get the url of the uploaded file?

Closed this issue · 1 comments

Hi! I just need help on how can i fetch/echo/var_dump the url path of the successfully uploaded file?

public function upload()
{
     Flysystem::put('hello.txt', 'Im using Amazon s3', ['ACL' => 'public-read']);

    $fileurl = how/to/getpath/of/the/successfullyuploaded/file?;

    return $fileurl;
}

please respond.

Thanks in advance.

Hey akubudoy,

this would upload to the root of the configured default bucket for S3. This means, that retrieving the URL is in theory possible by just appending the filename to the configured base_url, i.e.:

public function upload()
{
   $baseURL = config('flysystem.aws_s3.base_url');
    if (Flysystem::put('hello.txt', 'Im using Amazon s3', ['ACL' => 'public-read']))
    {
        return $baseURL . 'hello.txt';
    }
}

Remember to check if and how you set the base_url option in your flysystem config if you want to choose that solution. I would however recommend to just use Flysystem::get('hello.txt') to fetch the file. Another solution for S3 would be to pre-generate access URLs and access these from the object metadata with Flysystem::getMetadata('hello.txt').

You should, in any case, check the original Flysystem documentation for more information.