Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

t0berius's avatar

request file from amazon S3 and offer this file as download

Dear community,

what's the best way to offer a download to a user for a file stored on amazon S3? I'm using the laravel storage driver, uploading files to my S3 bucket is very easy, now I would like to offer users the possibility to download these files. After I saw in the doc I only get the content of the file I'm searching for a good way to offer a download from this blank content. I have no idea how to do so using the default laravel download return. Here's my part of storing the files, hope this helps to understand my problem:

switch ($request->file('attachment')->getMimeType()) 
                    {
                        case "image/jpeg":
                            $filename = $filename.'.jpg';
                            Storage::disk('s3')->put($filename, file_get_contents($request->file('attachment')));
                            break;
                        case "image/png":
                            $filename = $filename . '.png';
                            Storage::disk('s3')->put($filename, file_get_contents($request->file('attachment')));
                            break;
                        case "text/plain":
                            $filename = $filename . '.txt';
                            Storage::disk('s3')->put($filename, file_get_contents($request->file('attachment')));
                            break;
                        case "application/zip":
                            $filename = $filename . '.zip';
                            Storage::disk('s3')->put($filename, file_get_contents($request->file('attachment')));
                            break;
                        default:
                            $filename = $filename . '.txt';
                            Storage::disk('s3')->put($filename, file_get_contents($request->file('attachment')));
                           break;
                    }
0 likes
4 replies
rettigd's avatar

jaheller,

In your controller you can return pdf like so:

$headers = [
            'Content-Type'        => 'application/pdf',
            'Content-Disposition' => 'attachment; filename="filename.pdf"',
];
return Response::make(Storage::disk('s3')->get($filename), 200, $headers);
2 likes
t0berius's avatar

I've found a great way using pre signed URLs in S3.

Shahrukh4's avatar

You can give your Content-Type as desired and Content-Disposition as 'attachment' because your files are coming from S3 and you have to download it as an attachment.

$event_data = $this->ticket->where('user_id', $user_id)->first();
        
$data  = $event_data->pdf;

$get_ticket = 'tickets/'. $data;
$file_name  = "YOUR_DESIRED_NAME.pdf";

$headers = [
  'Content-Type'        => 'application/pdf',            
  'Content-Disposition' => 'attachment; filename="'. $file_name .'"',
];

return \Response::make(Storage::disk('s3')->get($get_ticket), 200, $headers);

Please or to participate in this conversation.