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

Custom filesystem provider to service multiple disks

我已经创建了自定义文件系统服务提供商来设置我的 google 云存储。我有多个存储桶,所以我在 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);
});

config/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)
        ],

使用 Laravel Flysystem 作为文件系统

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

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

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

但欢迎更明确的方法作为答案