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

Fix #60 - rewrite for createClient() #63

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Add a new disk to your `filesystems.php` config
'gcs' => [
'driver' => 'gcs',
'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
'key_file' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
'key_file_path' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
'key_file' => null, // optional: Array of data that substitutes the .json file (see below)
'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket
'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
Expand All @@ -44,7 +45,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 path in the key `key_file` in disk config, that json credentials file will be used.
1. If you specify a path in the key `key_file_path` 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 Down
10 changes: 10 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 2.2.2 - 2019-05-13

* Fix #60: `json key is missing the type field`.

* Support key_file_path and key_file separately (conforms with GCS class)

* Fix issue where in every case a key_file was given, breaking built-in service accounts

* Updated README with key_file, and key_file_path

## 2.2.1 - 2019-04-18

* Fix #57: `ErrorException thrown with message "array_merge(): Argument #2 is not an array"` when defining service account
Expand Down
37 changes: 24 additions & 13 deletions src/GoogleCloudStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class GoogleCloudStorageServiceProvider extends ServiceProvider
/**
* Create a Filesystem instance with the given adapter.
*
* @param \League\Flysystem\AdapterInterface $adapter
* @param array $config
* @param \League\Flysystem\AdapterInterface $adapter
* @param array $config
* @return \League\Flysystem\FlysystemInterfaceAdapterInterface
*/
protected function createFilesystem(AdapterInterface $adapter, array $config)
Expand All @@ -38,15 +38,15 @@ protected function createFilesystem(AdapterInterface $adapter, array $config)
/**
* Create a cache store instance.
*
* @param mixed $config
* @param mixed $config
* @return \League\Flysystem\Cached\CacheInterface
*
* @throws \InvalidArgumentException
*/
protected function createCacheStore($config)
{
if ($config === true) {
return new MemoryStore;
return new MemoryStore();
}

return new Cache(
Expand All @@ -62,7 +62,7 @@ protected function createCacheStore($config)
public function boot()
{
$factory = $this->app->make('filesystem');
/* @var FilesystemManager $factory */
// @var FilesystemManager $factory
$factory->extend('gcs', function ($app, $config) {
$storageClient = $this->createClient($config);

Expand All @@ -79,25 +79,37 @@ public function boot()
/**
* Create a new StorageClient
*
* @param mixed $config
* @param mixed $config
* @return \Google\Cloud\Storage\StorageClient
*/
private function createClient($config)
{
$keyFile = array_get($config, 'key_file');
if (is_string($keyFile)) {
//Get the keyfile config from the config/filesystems.php
$keyFilePath = array_get($config, 'key_file_path');

if (is_string($keyFilePath) && !empty($keyFilePath)) {
return new StorageClient([
'projectId' => $config['project_id'],
'keyFilePath' => $keyFile,
'keyFilePath' => $keyFilePath,
]);
}

if (! is_array($keyFile)) {
$keyFile = [];
//Get the keyFile array from the disk in config/filesystems.php
$keyFile = array_get($config, 'key_file');

if (is_array($keyFile) && !empty($keyFile)) {
return new StorageClient([
'projectId' => $config['project_id'],
]);
}

//If we don't have a keyFilePath or keyFile let
//the Google\Cloud\Storage\StorageClient try
//to authticate for us via Google App or
//Google Compute Engine instances or
//GOOGLE_APPLICATION_CREDENTIALS
return new StorageClient([
'projectId' => $config['project_id'],
'keyFile' => array_merge(["project_id" => $config['project_id']], $keyFile)
]);
}

Expand All @@ -106,6 +118,5 @@ private function createClient($config)
*/
public function register()
{
//
}
}