Download S3 file links in Laravel
Asked Answered
A

2

6

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

Aguilera answered 23/10, 2018 at 18:28 Comment(1)
Given that the file url is public, an alternative strategy is to send the download url of s3 through the API to your vue application and have the javascript initiate the download. See: #3749731Ascension
C
6

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

Crystallography answered 23/10, 2018 at 18:42 Comment(4)
$config->path means "https://s3-us-west-2.amazonaws.com/noetic-dev/2_Project/shiven-affordable-housing-surat/3_Document/Form+1/Form1.pdf" this url?Aguilera
yes..it may be different in your case.. depending on your model name $modelobject->pathtofileCrystallography
My Pleasure.. :)Crystallography
can we download directly from the URL by passing any argument to s3?Beatification
R
7

This code worked like a charm for downloading from S3 in Laravel 7 :

// $filePath should look like this: some-directory/filename.zip
return redirect(Storage::disk('s3')->temporaryUrl(
                    $filePath,
                    now()->addHour(),
                    ['ResponseContentDisposition' => 'attachment']
                ));

Credits to: https://sutherlandboswell.com/force-file-download-from-aws-s3-in-laravel/

Rewrite answered 19/4, 2020 at 13:15 Comment(0)
C
6

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

Crystallography answered 23/10, 2018 at 18:42 Comment(4)
$config->path means "https://s3-us-west-2.amazonaws.com/noetic-dev/2_Project/shiven-affordable-housing-surat/3_Document/Form+1/Form1.pdf" this url?Aguilera
yes..it may be different in your case.. depending on your model name $modelobject->pathtofileCrystallography
My Pleasure.. :)Crystallography
can we download directly from the URL by passing any argument to s3?Beatification

© 2022 - 2024 — McMap. All rights reserved.