Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

✨ Authentification without Json File #53

Merged
merged 6 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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

Expand Down
28 changes: 24 additions & 4 deletions src/GoogleCloudStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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', [])
]);
}
rommni marked this conversation as resolved.
Show resolved Hide resolved

/**
* Register bindings in the container.
Expand Down