Skip to content

Commit

Permalink
✨ Support for the new Google Cloud Storage Adapter (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- authored Oct 21, 2022
1 parent 517c234 commit 9a71235
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Adds :

- BC Break: Adding Types (remove deprecation notices triggered by Symfony)
_Please notice that the v0.8.0 is a major version, therefore if you extended any class or interface, you MUST implement it with types now._

- Support for Gaufrette 0.11
- With support for the new version of Google Client (compatible with PHP 8.0+)

Fixes :

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public function addConfiguration(NodeDefinition $builder): void
->children()
->scalarNode('directory')->defaultValue('')->end()
->scalarNode('acl')->defaultValue('private')->end()
->scalarNode('project_id')->end()
->scalarNode('bucket_location')->end()
->booleanNode('create')->defaultFalse()->end()
->end()
->end()
->end()
Expand Down
51 changes: 42 additions & 9 deletions Resources/docs/adapters/googlecloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Adapter for Google APIs Client Library for PHP.

```
composer require google/apiclient:^2.12
```

## Parameters

* `service_id` The service id of the `\Google_Service_Storage` to use. *(required)*
Expand All @@ -10,23 +14,52 @@ Adapter for Google APIs Client Library for PHP.
* `options` A list of additional options passed to the adapter.
* `directory` A directory to operate in. *(default '')*
* `acl` Whether the uploaded files should be `private` or `public` *(default `private`)*
* `create`: if `true`, gaufrette will create the bucket automatically *(default `false`)*
* `project_id`: required for automatic bucket creation
* `bucket_location`: required for automatic bucket creation

## Defining services

You need to create a custom factory service which creates a `\Google_Client` and authorizes with the correct scopes
and then returns a `\Google_Service_Storage` class connected to the client class:
You need to create a custom factory service which creates a `\Google\Client` and authorizes with the correct scopes
and then returns a `\Google\Service\Storage` class connected to the client class:

```yaml
services:
app.google_cloud_storage.service:
class: \Google_Service_Storage
factory: [App\Factory\GoogleCloudStorageServiceFactory
factory_method: 'createService'
arguments:
-
# all the arguments needed like service account email and path to key.p12
app.google_cloud_storage.service:
class: Google\Service\Storage
factory: ['App\Factory\GoogleCloudStorageServiceFactory', 'createGoogleCloudStorage']
```
The factory may be something like this:
```php
<?php

namespace App\Factory;

class GoogleCloudStorageServiceFactory
{
public static function createGoogleCloudStorage()
{
$keyFileLocation = '/path/to/key/project-id.json';
$bucketName = 'gaufrette-bucket-test-1234';
$projectId = 'some-project-586';
$bucketLocation = 'EUROPE-WEST9';

putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $keyFileLocation);
$client = new \Google\Client();
$client->setApplicationName('Gaufrette');
$client->addScope(\Google\Service\Storage::DEVSTORAGE_FULL_CONTROL);
$client->useApplicationDefaultCredentials();

return new \Google\Service\Storage($client);
}
}
```

⚠️ We do not recommend to set credentials directly in the factory, [read how to make a service factory with Symfony](https://symfony.com/doc/current/service_container/factories.html).


## Example

Once the service is set up use its key as the `service_id` in the gaufrette configuration:
Expand Down

0 comments on commit 9a71235

Please sign in to comment.