如何使用laravel 5.2 Storage::get()从amazons3获取URL不起作用


How to get URL from amazon s3 using laravel 5.2 Storage::get() not working

我可以使用Intervention将文件保存在我的amazons3中,现在我需要从s3获取我的图像的URL。如laravel 5.2所示,文档检索文件就是这样的,

$contents = Storage::get('file.jpg');

但当我尝试代码并将其转储时,却一无所获。我已经更改了存储桶中文件夹的权限。

这是我的代码:

    $file = Storage::get('products/3b2f81c6-7f73-4df8-a52f-82f60f50947a/cebadge.png');
    dd($file);

我安装了aws-sdk,现在将尝试使用它。我已经读过大多数关于这方面的文章,但我真的没有幸找到解决方案。有人能带领我走上正确的道路吗。非常感谢。

在s3中保存图像的代码,只需共享

$folderName = $request->input('folname')."/";
    $img = Image::make($file);
    $origName = $request->file('file')->getClientOriginalName();
 Storage::put($s3Bucket.'/'.$directory.'/'.$folderName.'/'.$origName, $img->__toString());

好吧,在阅读了大量AWS SDK for PHP文档和在线文章之后,我终于了解了它的工作原理,以及基本原理。

我的问题不是获取文件,而是将文件保存到s3中。

我使用上面的代码(见上面的问题)上传了一个文件。这是错误的代码。它怎么了?在将文件转换为字符串然后上传到s3之前,我没有将其转换为流。我所做的是将它直接转换为字符串并上传。这就是为什么我在使用storage::get()时一无所获的原因。

错误代码:

$img = Image::make($file);
    $origName = $request->file('file')->getClientOriginalName();
 Storage::put($s3Bucket.'/'.$directory.'/'.$folderName.'/'.$origName, $img->__toString());

还有什么问题。bucket名称,您不必指定bucket名称否则它将创建一个文件夹。

右侧代码:

$image = $request->file('file'); //the image
        $img = Image::make($file); // using interventation
 $image_normal = $img->stream(); // to stream
Storage::put($origName, $image_normal->__toString(),'public'); //to string

要使文件公开,只需包含第三个参数"public",否则它将自动使其私有。

总结一下。为您的网站上传和使用s3文件需要做的事情如下。

  1. 在AWS s3中创建您的帐户,只需按照laravel 5.2文档中的说明在配置''文件夹中设置filesystems.php

  2. 将默认存储设置为s3

"默认"=>"s3",

  1. 上传图像

    $origName = $request->file('file')->getClientOriginalName();
    $image = $request->file('file');
    $img = Image::make($file);
    $image_normal = $img ->stream();
    Storage::put($origName, $image_normal->__toString(),'public');
    
  2. 获取图像的URL就像下面的URL 一样简单

    https://s3-ap-southeast-1.amazonaws.com/mybucket/citibanko.PNG

    https://s3-[地区].amazonaws.com/[你的桶名]/filename.PNG

  3. 另外,如果你想在你的bucket中列出文件的名称,请使用以下代码:

    $files=存储::文件('/');转储($files);

对于文件夹

$files=存储::文件('oldername');转储($files);

在amazons3中,子目录实际上是不相关的,它只是bucket和文件名。