2

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']);
netdjw
  • 5,419
  • 21
  • 88
  • 162
  • You'll need to send `Content-Type` headers. – ceejayoz Feb 07 '21 at 18:07
  • Could you give me an example code, please? – netdjw Feb 07 '21 at 18:07
  • 1
    this question should help, [How to display image from aws s3 in blade laravel 5.2](https://stackoverflow.com/questions/39095691/how-to-display-image-from-aws-s3-in-blade-laravel-5-2) – bhucho Feb 07 '21 at 18:52
  • @bhucho thanks, but not really. This function has a route and not embedded into a blade template. – netdjw Feb 07 '21 at 18:54
  • you need to show what you are getting back from your get?, there is solution for that as well, if you can get mime-type from the Content-type header then you can pass the image as a variable as well – bhucho Feb 07 '21 at 18:55
  • use this answer as example https://stackoverflow.com/a/66077922/9471283 might help to tell how to convert you string to get image as a base64 string – bhucho Feb 07 '21 at 19:14

1 Answers1

4

The soultion was:

return response()->make(
    Storage::disk('s3')->get($path_and_filename),
    200,
    ['Content-Type' => 'image/jpeg']
);
netdjw
  • 5,419
  • 21
  • 88
  • 162