Skip to content

Commit 81dc2a2

Browse files
authored
Merge pull request #32 from dmstr-forks/feature/google-cloud
added Google Cloud Filesystem
2 parents 6d8e99f + bf441ae commit 81dc2a2

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/GoogleCloudFilesystem.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* @link https://github.com/creocoder/yii2-flysystem
4+
* @copyright Copyright (c) 2015 Alexander Kochetov
5+
* @license http://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
8+
namespace creocoder\flysystem;
9+
10+
use Google\Cloud\Storage\StorageClient;
11+
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter;
12+
use yii\base\InvalidConfigException;
13+
14+
/**
15+
* GoogleCloudFilesystem
16+
*
17+
* @author Tobias Munk <[email protected]>
18+
*/
19+
class GoogleCloudFilesystem extends Filesystem
20+
{
21+
/**
22+
* @var string
23+
*/
24+
public $projectId;
25+
/**
26+
* @var string
27+
*/
28+
public $keyFilePath;
29+
/**
30+
* @var string
31+
*/
32+
public $bucket;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function init()
38+
{
39+
if ($this->projectId === null) {
40+
throw new InvalidConfigException('The "projectId" property must be set.');
41+
}
42+
43+
if ($this->keyFilePath === null) {
44+
throw new InvalidConfigException('The "secret" property must be set.');
45+
}
46+
47+
if ($this->bucket === null) {
48+
throw new InvalidConfigException('The "bucket" property must be set.');
49+
}
50+
51+
parent::init();
52+
}
53+
54+
/**
55+
* @return GoogleStorageAdapter
56+
*/
57+
protected function prepareAdapter()
58+
{
59+
$config = [
60+
'projectId' => $this->projectId,
61+
'keyFilePath' => \Yii::getAlias($this->keyFilePath),
62+
];
63+
64+
$client = new StorageClient($config);
65+
$bucket = $client->bucket($this->bucket);
66+
return new GoogleStorageAdapter($client, $bucket);
67+
}
68+
}

0 commit comments

Comments
 (0)