How to return files from S3 bucket as image in Laravel?

2.6k views Asked by At

I started use an S3 bucket in my Laravel 8.x project, and I solved file uploads to the bucket with this code:

Storage::disk('s3')->put($file_path_on_s3_drive, $file_content_as_binary);

Now I try to serve this image to the browser like this:

public function showImage(string $id) {
    $image = Image::find($id);

    return Storage::disk('s3')->get($image->file_path_on_s3_drive);
}

But it shows the file as string in the browser and not as image.

How can show the data as image in the browser?

UPDATE: I call this showImage() function from a web route:

Route::get('image/{id}', [ImageController::class, 'showImage']);
1

There are 1 answers

0
netdjw On

The soultion was:

return response()->make(
    Storage::disk('s3')->get($path_and_filename),
    200,
    ['Content-Type' => 'image/jpeg']
);