laravellaravel-5.6laravel-storage

Download S3 file links in Laravel


I'm trying to build a small application in VueJs as frontend and Laravel as backend where I'm uploading files in my admin section to my aws-s3 while uploading the files I'm storing the link of that file in database. Every action is maintained by api calls, now I want to give option of these downloads to my end users so I'm making an axios call something like this:

downloadPDF(docs){
    const documents = {
        document: docs
    }
    axios.post('api/documents-download', documents, {headers: getHeader()}).then(response => {
        if(response.status === 200)
        {
            console.log('Downloaded')
        }
    })
},

And in my controller I'm having something like this:

public function download(Request $request)
{
    $headers = [
        'Content-Type' => 'application/pdf',
        'Content-Description' => 'File Transfer',
        'Content-Disposition' => "attachment; filename=filename.pdf",
    ];

    return response()->download($request->document, 'filename.pdf', $headers);
}

But it is throwing me error:

The file "https://s3-us-west-2.amazonaws.com/noetic-dev/2_Project/shiven-affordable-housing-surat/3_Document/Form+1/Form1.pdf" does not exist

This file clearly exists and made public as you can see above url shows the documents as linked.

Help me out with this. Thanks


Solution

  • i remember implementing this is in my project.. let me share a sample code with you... Laravel already provides details about s3Storage in documentation

    Code:

    use Illuminate\Support\Facades\Response as Download;
    
    public function download_config(Config $config)
        {
            $headers = [
                'Content-Type'        => 'Content-Type: application/zip',
                'Content-Disposition' => 'attachment; filename="'. $config->name .'"',
            ];
    
            return Download::make(Storage::disk('s3')->get($config->path), Response::HTTP_OK, $headers);
        }
    

    i'm supposing that you may be storing $config->path (path of file) this in your db. To learn more you can visit this