How to get file URL using Storage facade in laravel 5?

前端 未结 13 813
庸人自扰
庸人自扰 2020-11-30 22:40

I\'ve been experimenting using the new Flysystem integration with Laravel 5. I am storing \'localised\' paths to the DB, and getting the Storage facade to complete the path

13条回答
  •  难免孤独
    2020-11-30 23:24

    This is how I got it to work - switching between s3 and local directory paths with an environment variable, passing the path to all views.

    In .env:

    APP_FILESYSTEM=local or s3
    S3_BUCKET=BucketID
    

    In config/filesystems.php:

    'default' => env('APP_FILESYSTEM'),
    

    In app/Providers/AppServiceProvider:

    public function boot()
    {
        view()->share('dynamic_storage', $this->storagePath());
    }
    
    protected function storagePath()
    {
        if (Storage::getDefaultDriver() == 's3') {
            return Storage::getDriver()
                    ->getAdapter()
                    ->getClient()
                    ->getObjectUrl(env('S3_BUCKET'), '');
        }
    
        return URL::to('/');
    }
    

提交回复
热议问题