简体   繁体   English

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

[英]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:我创建了一种从 aws S3 获取数据(图像文件)的方法,如下所示:

 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.我确定这些图像存在于 aws 上,所以这没有问题。 And then I use above method as the src in my blade view like this:然后我使用上面的方法作为我的刀片视图中的 src,如下所示:

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

$myPath here already point to the specific file on aws for example: bucket/file.jgp.这里的$myPath 已经指向aws 上的特定文件例如:bucket/file.jgp。 But when I run my webpage, this Html::Image gives a view like this:但是当我运行我的网页时,这个 Html::Image 给出了这样的视图: 在此处输入图像描述

What's going on here?这里发生了什么? Why Html::image can't render my image file?为什么 Html::image 无法呈现我的图像文件? :( :(

适用于 Laravel 6.x

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

You can use Storage::url($imagePath) .您可以使用Storage::url($imagePath) Please see Filesystem / Cloud storage on laravel docs请参阅 laravel 文档上的文件系统/云存储

{!!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您可以添加像 follow 这样的访问器来获取图像 URL

It returns URL to the image and you can access it by $profile->image它返回图像的 URL,您可以通过 $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 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)这有效Storage::disk('s3')->url($imagePath)

and also from your Minio Browser.也来自您的 Minio 浏览器。

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

You have to Add a policy to Read and Write你必须Add一个策略来Read and Write

I think that private visibility of images is an important security feature.我认为图像的私有可见性是一项重要的安全功能。 I found a solution:-)我找到了解决方案:-)

Method in Controller : 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>

... ...

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

相关问题 如何使用 PHP 在我的网站上显示来自 AWS S3 存储桶的图像或视频文件 - How to display image or video file from AWS S3 bucket on my website using PHP 使用 Laravel 在 AWS S3 上上传后如何检索图像名称以存储在图像表中? - How to retrieve image's name to store in image table after uploading on AWS S3 using Laravel? 如何使用 PHP 显示来自 Amazon S3 的图像? - How do you display image from Amazon S3 with PHP? 如何在 AWS S3 托管的 static 网页上显示来自 DynamoDB 的数据 - How to display data from DynamoDB on an AWS S3 hosted static webpage 如何将图像从 django 应用程序自动上传到 aws s3 - How to auto-upload an image from django app to aws s3 将图像从 aws s3 存储桶加载到 django 应用程序 - Load an image from aws s3 bucket to django application 如何使用 aws cli 命令仅显示 s3 存储桶中的目录 - How to display only the directories from s3 bucket using aws cli command 如何使用节点、createPresignedPost 和获取将图像文件直接从客户端上传到 AWS S3 - How to upload an image file directly from client to AWS S3 using node, createPresignedPost, & fetch 无法从 aws s3 存储桶中删除图像 - Cannot delete image from aws s3 bucket Swift - AWS S3 从图片库上传图片并下载 - Swift - AWS S3 Upload Image from Photo Library and download it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM