Home > Back-end >  force download file from s3 url
force download file from s3 url

Time:08-04

I have an s3 URL without a file extension in the URL. is there any way I can force download file from the URL directly

https://s3-external-1.amazonaws.com/media.twiliocdn.com/AC451bab3cca7e01e20ee6bf1e746bed1f/10b62b6d5fe2f69be39840ce1201a2e7

In the database, I am storing this URL to display images or any file but now I have a requirement to download this image in user system on click of download button

FYI: this is not our s3 bucket but this is Twilio service bucket where our media is getting stored.

CodePudding user response:

From your controller, you're able to return a download response that lets you specify a file with a custom name. Like so...

    return response()
        ->download(
            $file,
            'download.jpeg',
        );

CodePudding user response:

You can temporarily store the image in your filesystem then send it as a download response.

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

$response = Http::get(
    'https://s3-external-1.amazonaws.com/media.twiliocdn.com/AC451bab3cca7e01e20ee6bf1e746bed1f/10b62b6d5fe2f69be39840ce1201a2e7'
);

file_put_contents($file = '/tmp/' . Str::uuid(), $response->body());

preg_match('/filename=\"(. )\"/', $response->header('content-disposition'), $matches);

return response()->download($file, $matches[1], [
    'Content-Type' => $response->header('content-type'),
])->deleteFileAfterSend();
  • Related