経緯
LaravelでGCSを利用するのにこれまでSuperbalist/laravel-google-cloud-storageを利用させてもらっていたが、GAEのPHPサポート最低バージョン変更に伴いLaravelのバージョンを上げる必要性が出てきた。
上記パッケージはしばらくメンテナンスされておらず、新しいLaravelでは使用できないため乗り換え先を探して置き換える。
乗り換え先
Providerの登録などもいらなさそうなので上記に決定
導入
パッケージインストール
composer require spatie/laravel-google-cloud-storage
既存configの確認
filesystems.php
'gcs' => [
'driver' => 'gcs',
'project_id' => env('GOOGLE_CLOUD_PROJECT_ID'),
'key_file' => app_storage_path(‘xxx/‘ . env('GOOGLE_CLOUD_KEY_FILE')),
'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET'),
'visibility' => 'private', // optional: public|private
]
configの書き換え
filesystems.php
'gcs' => [
'driver' => 'gcs',
'key_file_path' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
'key_file' => app_storage_path(‘xxx/‘ . env('GOOGLE_CLOUD_KEY_FILE')), // optional: Array of data that substitutes the .json file (see below)
'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'), // optional: is included in key file
'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', ''), // optional: /default/path/to/apply/in/bucket
'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
'apiEndpoint' => env('GOOGLE_CLOUD_STORAGE_API_ENDPOINT', null), // set storageClient apiEndpoint
'visibility' => 'public', // optional: public|private
'visibility_handler' => null, // optional: set to \League\Flysystem\GoogleCloudStorage\UniformBucketLevelAccessVisibility::class to enable uniform bucket level access
'metadata' => ['cacheControl'=> 'public,max-age=86400'], // optional: default metadata
],
動作確認
ここまでInstallationに従い、いざ確認してみるとエラーが…
PHP Notice: TypeError: Google\Auth\CredentialsLoader::makeCredentials(): Argument #2 ($jsonKey) must be of type array, string given, called in (省略)
なんか認証がうまくいってない??
同じように困ってる人がいないかissueを眺めていると、こんな記載を発見
change "key_file" to "key_file_path" in filesystems.php
ということで、key_fileをkey_file_pathに置き換えてもう一度確認
filesystems.php
'gcs' => [
'driver' => 'gcs',
'key_file_path' => app_storage_path('gcp/' . env('GOOGLE_CLOUD_KEY_FILE')), // optional: Array of data that substitutes the .json file (see below)
'project_id' => env('GOOGLE_CLOUD_PROJECT_ID'), // optional: is included in key file
'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET'),
'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', ''), // optional: /default/path/to/apply/in/bucket
'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI'), // see: Public URLs below
'apiEndpoint' => env('GOOGLE_CLOUD_STORAGE_API_ENDPOINT'), // set storageClient apiEndpoint
'visibility' => 'private', // optional: public|private
'visibility_handler' => null, // optional: set to \League\Flysystem\GoogleCloudStorage\UniformBucketLevelAccessVisibility::class to enable uniform bucket level access
'metadata' => ['cacheControl' => 'public,max-age=86400'], // optional: default metadata
'throw' => true,
],
無事動作しましたとさ