[英]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">
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.