简体   繁体   中英

How to display image from aws s3 in blade laravel 5.2

I have created a method for getting the data(image file) from aws S3 like this:

 public static function getImage ($imagePath)
    {
        if(Storage::exists($imagePath))
        {
            return Storage::disk('s3')->get($imagePath);
        }else
        {
            return 'No Image';
        }
    }

I'm sure that those image is exist on the aws, so there is no problem with that. And then I use above method as the src in my blade view like this:

{!!Html::image(getImage($myPath),'logo',['width'=>60,'height'=>55])!!}

$myPath here already point to the specific file on aws for example: bucket/file.jgp. But when I run my webpage, this Html::Image gives a view like this: 在此处输入图像描述

What's going on here? Why Html::image can't render my image file? :(

适用于 Laravel 6.x

<img src="{{Storage::disk('s3')->url($imagePath)}}">

You can use Storage::url($imagePath) . Please see Filesystem / Cloud storage on laravel docs

{!!Html::image(<Your S3 bucket URL>.$myPath),'logo',['width'=>60,'height'=>55])!!}

您可以将 S3 URL 保存在配置文件中并使用配置值而不是每次都写入实际值。

I had the same issue. This code snippet solved it

<img src="{!! url(<path to your image>) !!}" />

First, you need to give it public promotion in the controller

$path = Storage::disk('s3')->put('profileImages/', $request->file('profile_image'), 'public');

//save image name in database
$user->profile_image = basename($path);

In blade file

<img src="{{Storage::disk('s3')->url('profileImages/' . $user->profile_image)}}" />

You can add an accessor like follow to get Image URL

It returns URL to the image and you can access it by $profile->image

public function getImageAttribute($value)
{
    if ($value) {

        return Storage::disk('s3')->url($value);
    }

    return "https://source.unsplash.com/96x96/daily";
}

I assume you save image as follow

$path = $request->file('image')->store('images', 's3');
        
Storage::disk('s3')->setVisibility($path, 'public');

$profile->update([
      'image' =>  $path
      ]);

You can check this commit for more detail

https://github.com/itxshakil/ediary/commit/23cb4b1cb7b8a4eac68f534e8c5b6897abc6421a

I have read every solution and no one has pointed it directly. So I thought to write my answers. For this problem I just required to add this simple line and boom, it starts working. But I got access denied alert you should check that problem too.

<img class="img-responsive" src="{!! Storage::disk('s3')->url('thumbs/' . $business->image->path) !!}" alt="thumb">

This works Storage::disk('s3')->url($imagePath)

and also from your Minio Browser.

在此处输入图片说明 在此处输入图片说明

You have to Add a policy to Read and Write

I think that private visibility of images is an important security feature. I found a solution:-)

Method in Controller :

/**
* @param string $name : image name on bucket 
* ( stored if you want into database ) 
*/

public function show( $name ) {
    
   $path = config('filesystems.disks.s3.base_directory') . '/' . $name;
   $type = pathinfo($path, PATHINFO_EXTENSION);
   $data = Storage::get( $path );
   $base64 = 'data:image/' . $type . ';base64,' . base64_encode( $data );
            
   return view( 'show', [ 'src' => $base64 ]);        
}

The view:

...

<div class="panel panel-primary">
  <div class="panel-heading"><h2>Laravel File Retrive with Amazon S3 </h2></div>
  <div class="panel-body">

    **<img src="{{ $src }}"/>**

  </div>
</div>

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM