3
3
namespace Gaufrette \Adapter ;
4
4
5
5
use Gaufrette \Adapter ;
6
+ use Google \Service \Storage ;
7
+ use Google \Service \Storage \Bucket ;
8
+ use Google \Service \Storage \StorageObject ;
9
+ use Google \Service \Exception as ServiceException ;
10
+ use Google \Service \Storage \BucketIamConfiguration ;
11
+ use Google \Service \Storage \BucketIamConfigurationUniformBucketLevelAccess ;
6
12
use GuzzleHttp ;
7
13
8
14
/**
12
18
*/
13
19
class GoogleCloudStorage implements Adapter, MetadataSupporter, ListKeysAware
14
20
{
21
+ public const OPTION_CREATE_BUCKET_IF_NOT_EXISTS = 'create ' ;
22
+ public const OPTION_PROJECT_ID = 'project_id ' ;
23
+ public const OPTION_LOCATION = 'bucket_location ' ;
24
+ public const OPTION_STORAGE_CLASS = 'storage_class ' ;
25
+
15
26
protected $ service ;
16
27
protected $ bucket ;
17
- protected $ options ;
28
+ protected $ options = [
29
+ self ::OPTION_CREATE_BUCKET_IF_NOT_EXISTS => false ,
30
+ self ::OPTION_STORAGE_CLASS => 'STANDARD ' ,
31
+ 'directory ' => '' ,
32
+ 'acl ' => 'private ' ,
33
+ ];
18
34
protected $ bucketExists ;
19
35
protected $ metadata = [];
20
36
protected $ detectContentType ;
21
37
22
38
/**
23
- * @param \Google_Service_Storage $service The storage service class with authenticated
39
+ * @param Storage $service The storage service class with authenticated
24
40
* client and full access scope
25
41
* @param string $bucket The bucket name
26
42
* @param array $options Options can be directory and acl
27
43
* @param bool $detectContentType Whether to detect the content type or not
28
44
*/
29
45
public function __construct (
30
- \ Google_Service_Storage $ service ,
46
+ Storage $ service ,
31
47
$ bucket ,
32
48
array $ options = [],
33
49
$ detectContentType = false
34
50
) {
35
- if (!class_exists (\Google_Service_Storage ::class)) {
51
+ if (!class_exists (Storage ::class)) {
36
52
throw new \LogicException ('You need to install package "google/apiclient" to use this adapter ' );
37
53
}
38
54
39
55
$ this ->service = $ service ;
40
56
$ this ->bucket = $ bucket ;
41
57
$ this ->options = array_replace (
42
- [
43
- 'directory ' => '' ,
44
- 'acl ' => 'private ' ,
45
- ],
58
+ $ this ->options ,
46
59
$ options
47
60
);
48
61
@@ -80,6 +93,7 @@ public function getBucket()
80
93
*/
81
94
public function setBucket ($ bucket )
82
95
{
96
+ $ this ->bucketExists = null ;
83
97
$ this ->bucket = $ bucket ;
84
98
}
85
99
@@ -145,7 +159,7 @@ public function write($key, $content)
145
159
unset($ metadata ['ContentType ' ]);
146
160
}
147
161
148
- $ object = new \ Google_Service_Storage_StorageObject ();
162
+ $ object = new StorageObject ();
149
163
$ object ->name = $ path ;
150
164
151
165
if (isset ($ metadata ['ContentDisposition ' ])) {
@@ -182,7 +196,7 @@ public function write($key, $content)
182
196
}
183
197
184
198
return $ object ->getSize ();
185
- } catch (\ Google_Service_Exception $ e ) {
199
+ } catch (ServiceException $ e ) {
186
200
return false ;
187
201
}
188
202
}
@@ -197,7 +211,7 @@ public function exists($key)
197
211
198
212
try {
199
213
$ this ->service ->objects ->get ($ this ->bucket , $ path );
200
- } catch (\ Google_Service_Exception $ e ) {
214
+ } catch (ServiceException $ e ) {
201
215
return false ;
202
216
}
203
217
@@ -235,7 +249,7 @@ public function delete($key)
235
249
236
250
try {
237
251
$ this ->service ->objects ->delete ($ this ->bucket , $ path );
238
- } catch (\ Google_Service_Exception $ e ) {
252
+ } catch (ServiceException $ e ) {
239
253
return false ;
240
254
}
241
255
@@ -259,7 +273,7 @@ public function rename($sourceKey, $targetKey)
259
273
try {
260
274
$ this ->service ->objects ->copy ($ this ->bucket , $ sourcePath , $ this ->bucket , $ targetPath , $ object );
261
275
$ this ->service ->objects ->delete ($ this ->bucket , $ sourcePath );
262
- } catch (\ Google_Service_Exception $ e ) {
276
+ } catch (ServiceException $ e ) {
263
277
return false ;
264
278
}
265
279
@@ -301,7 +315,7 @@ public function listKeys($prefix = '')
301
315
$ reflectionProperty ->setAccessible (true );
302
316
$ reflectionProperty ->setValue ($ list , 'items ' );
303
317
304
- /** @var \Google_Service_Storage_StorageObject $object */
318
+ /** @var StorageObject $object */
305
319
foreach ($ list as $ object ) {
306
320
$ keys [] = $ object ->name ;
307
321
}
@@ -347,7 +361,39 @@ protected function ensureBucketExists()
347
361
$ this ->bucketExists = true ;
348
362
349
363
return ;
350
- } catch (\Google_Service_Exception $ e ) {
364
+ } catch (ServiceException $ e ) {
365
+ if ($ this ->options [self ::OPTION_CREATE_BUCKET_IF_NOT_EXISTS ]) {
366
+ if (!isset ($ this ->options [self ::OPTION_PROJECT_ID ])) {
367
+ throw new \RuntimeException (
368
+ sprintf ('Option "%s" missing, cannot create bucket ' , self ::OPTION_PROJECT_ID )
369
+ );
370
+ }
371
+ if (!isset ($ this ->options [self ::OPTION_LOCATION ])) {
372
+ throw new \RuntimeException (
373
+ sprintf ('Option "%s" missing, cannot create bucket ' , self ::OPTION_LOCATION )
374
+ );
375
+ }
376
+
377
+ $ bucketIamConfigDetail = new BucketIamConfigurationUniformBucketLevelAccess ();
378
+ $ bucketIamConfigDetail ->setEnabled (true );
379
+ $ bucketIam = new BucketIamConfiguration ();
380
+ $ bucketIam ->setUniformBucketLevelAccess ($ bucketIamConfigDetail );
381
+ $ bucket = new Bucket ();
382
+ $ bucket ->setName ($ this ->bucket );
383
+ $ bucket ->setLocation ($ this ->options [self ::OPTION_LOCATION ]);
384
+ $ bucket ->setStorageClass ($ this ->options [self ::OPTION_STORAGE_CLASS ]);
385
+ $ bucket ->setIamConfiguration ($ bucketIam );
386
+
387
+ $ this ->service ->buckets ->insert (
388
+ $ this ->options [self ::OPTION_PROJECT_ID ],
389
+ $ bucket
390
+ );
391
+
392
+ $ this ->bucketExists = true ;
393
+
394
+ return ;
395
+ }
396
+
351
397
$ this ->bucketExists = false ;
352
398
353
399
throw new \RuntimeException (
@@ -372,13 +418,13 @@ protected function computePath($key)
372
418
* @param string $path
373
419
* @param array $options
374
420
*
375
- * @return bool|\Google_Service_Storage_StorageObject
421
+ * @return bool|StorageObject
376
422
*/
377
423
private function getObjectData ($ path , $ options = [])
378
424
{
379
425
try {
380
426
return $ this ->service ->objects ->get ($ this ->bucket , $ path , $ options );
381
- } catch (\ Google_Service_Exception $ e ) {
427
+ } catch (ServiceException $ e ) {
382
428
return false ;
383
429
}
384
430
}
0 commit comments