繁体   English   中英

如何在刀片 laravel 5.2 中显示来自 aws s3 的图像

[英]How to display image from aws s3 in blade laravel 5.2

我创建了一种从 aws S3 获取数据(图像文件)的方法,如下所示:

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

我确定这些图像存在于 aws 上,所以这没有问题。 然后我使用上面的方法作为我的刀片视图中的 src,如下所示:

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

这里的$myPath 已经指向aws 上的特定文件例如:bucket/file.jgp。 但是当我运行我的网页时,这个 Html::Image 给出了这样的视图: 在此处输入图像描述

这里发生了什么? 为什么 Html::image 无法呈现我的图像文件? :(

适用于 Laravel 6.x

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

您可以使用Storage::url($imagePath) 请参阅 laravel 文档上的文件系统/云存储

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

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

我有同样的问题。 此代码片段解决了它

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

首先,您需要在控制器中对其进行公开宣传

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

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

在刀片文件中

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

您可以添加像 follow 这样的访问器来获取图像 URL

它返回图像的 URL,您可以通过 $profile->image 访问它

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

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

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

我假设您按如下方式保存图像

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

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

您可以检查此提交以获取更多详细信息

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

我已经阅读了所有解决方案,但没有人直接指出。 所以我想写下我的答案。 对于这个问题,我只需要添加这条简单的线和动臂,它就开始工作了。 但是我收到了拒绝访问警报,您也应该检查该问题。

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

这有效Storage::disk('s3')->url($imagePath)

也来自您的 Minio 浏览器。

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

你必须Add一个策略来Read and Write

我认为图像的私有可见性是一项重要的安全功能。 我找到了解决方案:-)

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 ]);        
}

风景:

...

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

...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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