|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace diecoding\flysystem; |
|
4
|
|
|
|
|
5
|
|
|
use Google\Auth\FetchAuthTokenInterface; |
|
6
|
|
|
use Google\Cloud\Storage\StorageClient; |
|
7
|
|
|
use League\Flysystem\GoogleCloudStorage\GoogleCloudStorageAdapter; |
|
8
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
9
|
|
|
use yii\base\InvalidConfigException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Interacting with Google Cloud Storage filesystem |
|
13
|
|
|
* @see https://flysystem.thephpleague.com/docs/adapter/google-cloud-storage/ |
|
14
|
|
|
* |
|
15
|
|
|
* ```php |
|
16
|
|
|
* 'components' => [ |
|
17
|
|
|
* 'fs' => [ |
|
18
|
|
|
* 'class' => \diecoding\flysystem\GoogleCloudStorageComponent::class, |
|
19
|
|
|
* 'bucket' => 'your-bucket', |
|
20
|
|
|
* // 'apiEndpoint' => '', |
|
21
|
|
|
* // 'projectId' => '', |
|
22
|
|
|
* // 'authCache' => null, |
|
23
|
|
|
* // 'authCacheOptions' => [], |
|
24
|
|
|
* // 'authHttpHandler' => function () {}, |
|
25
|
|
|
* // 'credentialsFetcher' => null, |
|
26
|
|
|
* // 'httpHandler' => function () {}, |
|
27
|
|
|
* // 'keyFile' => '', |
|
28
|
|
|
* 'keyFilePath' => __DIR__ . '/gcs_credentials.json', |
|
29
|
|
|
* // 'requestTimeout' => 0, |
|
30
|
|
|
* // 'retries' => 0, |
|
31
|
|
|
* // 'scopes' => [], |
|
32
|
|
|
* // 'quotaProject' => '', |
|
33
|
|
|
* // 'userProject' => false, |
|
34
|
|
|
* // 'debug' => false, |
|
35
|
|
|
* // 'prefix' => '', |
|
36
|
|
|
* ], |
|
37
|
|
|
* ], |
|
38
|
|
|
* ``` |
|
39
|
|
|
* |
|
40
|
|
|
* @link https://sugengsulistiyawan.my.id/ |
|
41
|
|
|
* @author Sugeng Sulistiyawan <[email protected]> |
|
42
|
|
|
* @copyright Copyright (c) 2024 |
|
43
|
|
|
*/ |
|
44
|
|
|
class GoogleCloudStorageComponent extends AbstractComponent |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @var string The name of the bucket to request. |
|
48
|
|
|
*/ |
|
49
|
|
|
public $bucket; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string The hostname with optional port to use in |
|
53
|
|
|
* place of the default service endpoint. Example: |
|
54
|
|
|
* `foobar.com` or `foobar.com:1234`. |
|
55
|
|
|
*/ |
|
56
|
|
|
public $apiEndpoint; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string The project ID from the Google Developer's |
|
60
|
|
|
* Console. |
|
61
|
|
|
*/ |
|
62
|
|
|
public $projectId; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var CacheItemPoolInterface A cache used storing access |
|
66
|
|
|
* tokens. **Defaults to** a simple in memory implementation. |
|
67
|
|
|
*/ |
|
68
|
|
|
public $authCache; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var array Cache configuration options. |
|
72
|
|
|
*/ |
|
73
|
|
|
public $authCacheOptions; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var callable A handler used to deliver Psr7 |
|
77
|
|
|
* requests specifically for authentication. |
|
78
|
|
|
*/ |
|
79
|
|
|
public $authHttpHandler; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var FetchAuthTokenInterface A credentials fetcher instance. |
|
83
|
|
|
*/ |
|
84
|
|
|
public $credentialsFetcher; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var callable A handler used to deliver Psr7 requests. |
|
88
|
|
|
* Only valid for requests sent over REST. |
|
89
|
|
|
*/ |
|
90
|
|
|
public $httpHandler; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @var array The contents of the service account credentials |
|
94
|
|
|
* .json file retrieved from the Google Developer's Console. |
|
95
|
|
|
* Ex: `json_decode(file_get_contents($path), true)`. |
|
96
|
|
|
*/ |
|
97
|
|
|
public $keyFile; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @var string The full path to your service account |
|
101
|
|
|
* credentials .json file retrieved from the Google Developers |
|
102
|
|
|
* Console. |
|
103
|
|
|
*/ |
|
104
|
|
|
public $keyFilePath; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @var float Seconds to wait before timing out the |
|
108
|
|
|
* request. **Defaults to** `0` with REST and `60` with gRPC. |
|
109
|
|
|
*/ |
|
110
|
|
|
public $requestTimeout; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var int Number of retries for a failed request. |
|
114
|
|
|
* **Defaults to** `3`. |
|
115
|
|
|
*/ |
|
116
|
|
|
public $retries; |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @var array Scopes to be used for the request. |
|
120
|
|
|
*/ |
|
121
|
|
|
public $scopes; |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @var string Specifies a user project to bill for |
|
125
|
|
|
* access charges associated with the request. |
|
126
|
|
|
*/ |
|
127
|
|
|
public $quotaProject; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @var string|bool $userProject If true, the current Project ID |
|
131
|
|
|
* will be used. If a string, that string will be used as the |
|
132
|
|
|
* userProject argument, and that project will be billed for the |
|
133
|
|
|
* request. **Defaults to** `false`. |
|
134
|
|
|
*/ |
|
135
|
|
|
public $userProject = false; |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @var StorageClient |
|
139
|
|
|
*/ |
|
140
|
|
|
protected $client; |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @inheritdoc |
|
144
|
|
|
*/ |
|
145
|
|
|
public function init() |
|
146
|
|
|
{ |
|
147
|
|
|
if (empty($this->bucket)) { |
|
148
|
|
|
throw new InvalidConfigException('The "bucket" property must be set.'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
parent::init(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return GoogleCloudStorageAdapter |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function initAdapter() |
|
158
|
|
|
{ |
|
159
|
|
|
$config = [ |
|
160
|
|
|
'apiEndpoint' => $this->apiEndpoint, |
|
161
|
|
|
'projectId' => $this->projectId, |
|
162
|
|
|
'authCache' => $this->authCache, |
|
163
|
|
|
'authCacheOptions' => $this->authCacheOptions, |
|
164
|
|
|
'authHttpHandler' => $this->authHttpHandler, |
|
165
|
|
|
'credentialsFetcher' => $this->credentialsFetcher, |
|
166
|
|
|
'httpHandler' => $this->httpHandler, |
|
167
|
|
|
'keyFile' => $this->keyFile, |
|
168
|
|
|
'keyFilePath' => $this->keyFilePath, |
|
169
|
|
|
'requestTimeout' => $this->requestTimeout, |
|
170
|
|
|
'retries' => $this->retries, |
|
171
|
|
|
'scopes' => $this->scopes, |
|
172
|
|
|
'quotaProject' => $this->quotaProject, |
|
173
|
|
|
]; |
|
174
|
|
|
|
|
175
|
|
|
foreach ($config as $key => $value) { |
|
176
|
|
|
if (empty($value)) { |
|
177
|
|
|
unset($config[$key]); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$this->client = new StorageClient($config); |
|
182
|
|
|
$bucket = $this->client->bucket($this->bucket, $this->userProject); |
|
183
|
|
|
|
|
184
|
|
|
return new GoogleCloudStorageAdapter($bucket, (string) $this->prefix); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|