Skip to content

Commit eadb134

Browse files
authored
Merge pull request #557 from KnpLabs/feature/google-cloud-client-adapter
[RFR] Feature/google cloud client adapter
2 parents 2570678 + 320deeb commit eadb134

File tree

9 files changed

+550
-26
lines changed

9 files changed

+550
-26
lines changed

.env.dist

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ AWS_KEY=
22
AWS_SECRET=
33
AWS_BUCKET=
44

5+
# see /doc/adapters/google-cloud-storage.md
6+
GCS_PROJECT_ID=
7+
GCS_BUCKET_NAME=
8+
GCS_JSON_KEY_FILE=/app/secrets/gaufrette-appId.json
9+
510
MONGO_URI=mongodb://mongodb:27017
611
MONGO_DBNAME=gridfs_test
712

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ bin/*
22
!bin/tests
33
!bin/tests-all
44
doc/.couscous
5+
tests/adapters/*
6+
!tests/adapters/*.dist
57
vendor/
68
.env
79
composer.lock
810
phpunit.xml
11+
secrets/

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
v1.0.0
2+
======
3+
4+
## New features
5+
6+
- Google Cloud Storage Adapter (#557)
7+
8+
Thank you @nicolasmure and @PanzerLlama for your contributions !
9+
110
v0.8.3
211
======
312

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Requires the latest versions of :
7272
```bash
7373
$ make dev
7474
```
75-
and configure it as you want.
75+
and fill it with the credentials of the backend you want to use.
7676

7777
2) Build the php docker image :
7878
```bash

composer.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"mongodb/mongodb": "^1.1",
3737
"microsoft/azure-storage-blob": "^1.0",
3838
"akeneo/phpspec-skip-example-extension": "^4.0",
39-
"liuggio/fastest": "^1.6"
39+
"liuggio/fastest": "^1.6",
40+
"google/cloud-storage": "~1.0"
4041
},
4142
"suggest": {
4243
"knplabs/knp-gaufrette-bundle": "to use with Symfony",
@@ -51,10 +52,12 @@
5152
"gaufrette/opencloud-adapter": "to use Opencloud adapter",
5253
"gaufrette/zip-adapter": "to use Zip adapter",
5354
"gaufrette/gridfs-adapter": "to use GridFS adapter",
54-
"google/apiclient": "to use GoogleCloudStorage adapter",
55+
"google/cloud-storage": "to use GoogleCloudStorage adapter",
5556
"ext-curl": "*",
5657
"ext-mbstring": "*",
57-
"ext-fileinfo": "This extension is used to automatically detect the content-type of a file in the AwsS3, OpenCloud, AzureBlogStorage and GoogleCloudStorage adapters"
58+
"ext-fileinfo": "This extension is used to automatically detect the content-type of a file in the AwsS3, OpenCloud, AzureBlogStorage and GoogleCloudStorage adapters",
59+
"ext-mongodb": "*",
60+
"mongodb/mongodb": "*"
5861
},
5962
"autoload": {
6063
"psr-0": { "Gaufrette": "src/" }

doc/adapters/google-cloud-storage.md

+50-22
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
currentMenu: google-cloud-storage
33
---
44

5-
# GoogleCloudStorage
5+
# Google Cloud Storage
66

7-
To use the GoogleCloudStorage adapter you will need to create a connection using the [Google APIs Client Library for PHP]
8-
(https://github.com/google/google-api-php-client) and create a Client ID/Service Account in your [Developers Console]
9-
(https://console.developers.google.com/). You can then create the `\Google_Service_Storage` which is required for the
10-
GoogleCloudStorage adapter.
7+
This adapter requires an instance of `Google\Cloud\Storage\StorageClient`
8+
that has proper access rights to the bucket you want to use.
9+
10+
For more details see:
11+
http://googlecloudplatform.github.io/google-cloud-php/
12+
https://console.cloud.google.com/
13+
14+
In order to get started:
15+
16+
1) Create a project in [Google Cloud Platform](https://console.cloud.google.com/).
17+
2) Create a bucket for the project in Storage.
18+
3) Create a Service Account in IAM & Admin section that can write access the bucket, download its key.json file.
19+
20+
**At all times make sure you keep your key.json file private and nobody can access it from the Internet.**
1121

1222
## Example
1323

@@ -16,24 +26,42 @@ GoogleCloudStorage adapter.
1626

1727
use Gaufrette\Filesystem;
1828
use Gaufrette\Adapter\GoogleCloudStorage;
29+
use Google\Cloud\Storage\StorageClient;
30+
31+
$storage = new StorageClient(array(
32+
'projectId' => 'your-project-id',
33+
'keyFilePath' => 'path/to/your/project/key.json'
34+
));
1935

20-
$client = new \Google_Client();
21-
$client->setClientId('xxxxxxxxxxxxxxx.apps.googleusercontent.com');
22-
$client->setApplicationName('Gaufrette');
36+
# You can optionally set the directory in the bucket and the acl permissions for all uploaded files...
37+
# By default Cloud Storage applies the bucket's default object ACL to the object (uploaded file).
38+
# The example below gives read access to the uploaded files to anyone in the world
39+
# Note that the public URL of the file IS NOT the bucket's file url,
40+
# see https://cloud.google.com/storage/docs/access-public-data for details
2341

24-
$cred = new \Google_Auth_AssertionCredentials(
25-
26-
array(\Google_Service_Storage::DEVSTORAGE_FULL_CONTROL),
27-
file_get_contents('key.p12')
42+
$adapter = new GoogleCloudStorage($storage, 'bucket_name',
43+
array(
44+
'directory' => 'bucket_directory',
45+
'acl' => array(
46+
'allUsers' => \Google\Cloud\Storage\Acl::ROLE_READER
47+
)
48+
)
49+
);
50+
51+
$key = 'myAmazingFile.txt';
52+
53+
$filesystem = new Filesystem($adapter);
54+
55+
$filesystem->write($key, 'Uploaded at: '.date('Y-m-d @ H:i:s'), true);
56+
57+
# optional
58+
$adapter->setMetadata($key,
59+
array(
60+
'FileDescription' => 'This is my file. There are many like it, but this one is mine.'
61+
)
2862
);
29-
$client->setAssertionCredentials($cred);
30-
if ($client->getAuth()->isAccessTokenExpired()) {
31-
$client->getAuth()->refreshTokenWithAssertion($cred);
32-
}
33-
34-
$service = new \Google_Service_Storage($client);
35-
$adapter = new Gaufrette\Adapter\GoogleCloudStorage($service, $config['gcsBucket'], array(
36-
'acl' => 'public',
37-
), true);
38-
$filesystem = new Gaufrette\Filesystem($adapter);
3963
```
64+
65+
Here you can find some more info regarding ACL:
66+
* [Creating and Managing Access Control Lists (ACLs)](https://cloud.google.com/storage/docs/access-control/create-manage-lists)
67+
* [Access Control Lists (ACLs)](https://cloud.google.com/storage/docs/access-control/lists)

secrets/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)