diff --git a/README.md b/README.md index b869810..bd81b0d 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Add a new disk to your `filesystems.php` config The Google Client uses a few methods to determine how it should authenticate with the Google API. -1. If you specify a `key_file` in the disk config, that json credentials file will be used. +1. If you specify a path in the key `key_file` in disk config, that json credentials file will be used. 2. If the `GOOGLE_APPLICATION_CREDENTIALS` env var is set, it will use that. ```php putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); @@ -55,6 +55,20 @@ The Google Client uses a few methods to determine how it should authenticate wit 4. If running in **Google App Engine**, the built-in service account associated with the application will be used. 5. If running in **Google Compute Engine**, the built-in service account associated with the virtual machine instance will be used. +6. If you want to authenticate directly without using a json file, you can specify an array for `key_file` in disk config with this data: + ```php + 'key_file' => [ + 'type' => env('GOOGLE_CLOUD_ACCOUNT_TYPE'), + 'private_key_id' => env('GOOGLE_CLOUD_PRIVATE_KEY_ID'), + 'private_key' => env('GOOGLE_CLOUD_PRIVATE_KEY'), + 'client_email' => env('GOOGLE_CLOUD_CLIENT_EMAIL'), + 'client_id' => env('GOOGLE_CLOUD_CLIENT_ID'), + 'auth_uri' => env('GOOGLE_CLOUD_AUTH_URI'), + 'token_uri' => env('GOOGLE_CLOUD_TOKEN_URI'), + 'auth_provider_x509_cert_url' => env('GOOGLE_CLOUD_AUTH_PROVIDER_CERT_URL'), + 'client_x509_cert_url' => env('GOOGLE_CLOUD_CLIENT_CERT_URL'), + ], + ``` ### Public URLs diff --git a/src/GoogleCloudStorageServiceProvider.php b/src/GoogleCloudStorageServiceProvider.php index a6e83fc..4dce843 100644 --- a/src/GoogleCloudStorageServiceProvider.php +++ b/src/GoogleCloudStorageServiceProvider.php @@ -64,10 +64,8 @@ public function boot() $factory = $this->app->make('filesystem'); /* @var FilesystemManager $factory */ $factory->extend('gcs', function ($app, $config) { - $storageClient = new StorageClient([ - 'projectId' => $config['project_id'], - 'keyFilePath' => array_get($config, 'key_file'), - ]); + $storageClient = $this->createClient($config); + $bucket = $storageClient->bucket($config['bucket']); $pathPrefix = array_get($config, 'path_prefix'); $storageApiUri = array_get($config, 'storage_api_uri'); @@ -78,6 +76,28 @@ public function boot() }); } + /** + * Create a new StorageClient + * + * @param mixed $config + * @return \Google\Cloud\Storage\StorageClient + */ + private function createClient($config) + { + if (is_string(array_get($config, 'key_file'))) { + return new StorageClient([ + 'projectId' => $config['project_id'], + 'keyFilePath' => array_get($config, 'key_file'), + ]); + } + + return new StorageClient([ + 'projectId' => $config['project_id'], + 'keyFile' => array_merge([ + "project_id" => $config['project_id'] + ], array_get($config, 'key_file', []) + ]); + } /** * Register bindings in the container.