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