Laravel文件系统/存储-本地/公共文件URL不起作用 [英] Laravel Filesystem/Storage - Local/Public File URLs Not Working

查看:434
本文介绍了Laravel文件系统/存储-本地/公共文件URL不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

您好,我将laravel用于RESTful API. 我想向用户返回一个指向图像文件的URL. 我当前正在使用本地存储,但是我已经配置了一个Amazon S3实例,并将其图像同步到该实例. 我的目标是只需更改config/filesystems.php中的第一行,即可在本地存储和s3之间无缝切换:

Hi, I am using laravel for a RESTful API. I would like to return users a URL to an image file. I am currently using local storage, but I have configured an Amazon S3 instance, and synced my images to it. My aim is to seamlessly switch between local storage and s3 by simply changing the first line in my config/filesystems.php:

'default' => 'public',

详细信息

我已配置laravel通过config/filesystems.php文件访问本地磁盘和S3. 按照laravel的说明,我已将public/storage链接到storage/app/public. 这是我的config/filesystems.php中的磁盘阵列:

I have configured laravel to access both the local disk and S3 via the config/filesystems.php file. As per the laravel instructions I have softlinked public/storage to storage/app/public. Here is the disks array from my config/filesystems.php:

    'disks' => [

//        'local' => [
//            'driver'    => 'local',
//            'root'      => storage_path('app'),
//        ],

        'public' => [
            'driver'        => 'local',
            'root'          => storage_path('app/public'),
            'visibility'    => 'public',
        ],

        's3' => [
            'driver'    => 's3',
            'key'       => '...',
            'secret'    => '...',
            'region'    => '...',
            'bucket'    => 'mybucket',
        ],
    ],

这是我的文件夹层次结构:

This is my folder hierarchy:

在本地磁盘上:

  • myLaravelApp/storage/mobile/backgrounds/
  • myLaravelApp/存储/移动/closetimages/
  • myLaravelApp/存储/移动/profileimages/

在S3上

  • mybucket/mobile/backgrounds/
  • mybucket/mobile/closetimages/
  • mybucket/mobile/profileimages/

如前所述,我想通过简单地更改config/filesystems.php中的第一行来在本地存储和s3之间无缝切换:

As I said earlier, I would like to seamlessly switch between local storage and s3 by simply changing the first line in my config/filesystems.php:

'default' => 'public',

我希望使用以下函数调用将数据返回给用户:

and I was hoping to use the following function call to return data to the user:

return Storage::url('mobile/profileimages/12345.jpg');

当我使用s3作为默认通话时,响应如下:

When I make this call with s3 as default, the response is like this:

https://s3.amazonaws.com/mybucket/mobile/profileimages/12345.jpg

哪个才华横溢!但是,当我使用默认本地存储进行此调用时,响应为:

Which is brilliant! However when I make this call with default local storage, the response is:

/storage/mobile/profileimages/12345.jpg

/storage/mobile/profileimages/12345.jpg

甚至还不是一个完整的URL :(我想返回的是这样的:

Which is not even a complete URL :( What I would like to return is something like:

http://mywebapp/storage/mobile/profileimages/12345.jpg

但是我希望同一个呼叫可同时用于s3和本地存储,以便我可以无缝切换.

But I would like the same call to work for both s3 and local storage so that I can switch seamlessly.

我使用不正确吗?令人沮丧的是,这显然是一个库/框架调用,因此我希望它可以运行,或者至少返回完整的URL.

Am I using it incorrectly? It's frustrating because this is obviously a library/framework call so I would expect it to work, or at least to return a complete URL.

谢谢

推荐答案

您可以尝试类似的操作

function public_url($path = '')
{
    $fs = app('filesystem');

    if ($fs->getDriver()->getAdapter() instanceof \League\Flysystem\Adapter\Local) {
        return asset($fs->url($path));
    }

    return $fs->url($path);
}

您可以像这样使用它

$assetUrl = public_url('mobile/profileimages/12345.jpg');

这篇关于Laravel文件系统/存储-本地/公共文件URL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆