How to display image from aws s3 in blade laravel 5.2

两盒软妹~` 提交于 2021-02-18 17:10:51

问题


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 ? :(


回答1:


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

You can save the S3 URL in a config file and use the config value instead of writing actual value everytime.




回答2:


Works on Laravel 6.x

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



回答3:


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




回答4:


I had the same issue. This code snippet solved it

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



回答5:


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




回答6:


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">



回答7:


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)}}" />


来源:https://stackoverflow.com/questions/39095691/how-to-display-image-from-aws-s3-in-blade-laravel-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!