自定义文件系统提供程序以服务多个磁盘

时间:2017-06-29 10:31:46

标签: php laravel laravel-storage

我已经创建了自定义文件系统服务提供商来设置我的谷歌云存储。我有多个存储桶,所以我在filesystems.php中创建了多个磁盘。但由于所有这些存储桶都使用类似的设置,我想使用单一服务提供商。我们能实现吗?

ServicePrivider boot()

Storage::extend('gcs_images_1', function ($app, $config) {
    $client = new StorageClient([
        'projectId' => $config['project_id'],
        'keyFilePath' => storage_path($config['key_file'])
    ]);

    $bucket = $client->bucket($config['bucket']);

    $pathPrefix = array_get($config, 'path_prefix');

    $storageApiUri = array_get($config, 'storage_api_uri');

    $adapter = new GoogleStorageAdapter($client, $bucket, $pathPrefix, $storageApiUri);

    return new Filesystem($adapter);
});

配置/ filesystems.php

'gcs_images_1' => [
            'driver' => 'gcs_images_1',
            'project_id' => env('GOOGLE_CLOUD_PROJECT_ID'),
            'key_file' => env('GOOGLE_CLOUD_KEY_FILE'),
            'bucket' => 'img',
            'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null),
            'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null)
        ],
        'gcs_docs_1' => [
            'driver' => 'gcs_docs_1',
            'project_id' => env('GOOGLE_CLOUD_PROJECT_ID'),
            'key_file' => env('GOOGLE_CLOUD_KEY_FILE'),
            'bucket' => 'doc',
            'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null),
            'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null)
        ],

2 个答案:

答案 0 :(得分:1)

将Laravel Flysystem用于文件系统

https://github.com/GrahamCampbell/Laravel-Flysystem

答案 1 :(得分:1)

所以解决方案很简单。返回文件系统实例数组

        $fileSystems = array();
        foreach ($this->gcsDisks as $disk) {
            $fileSystems[] = $this->getFileSystem($disk);
        }
        return $fileSystems;

但更明确的方法是做到这一点,这是一个值得欢迎的答案