1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP-FFmpeg-video-streaming package. |
5
|
|
|
* |
6
|
|
|
* (c) Amin Yazdanpanah <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
namespace Streaming\Clouds; |
14
|
|
|
|
15
|
|
|
use Google\Cloud\Storage\StorageClient; |
16
|
|
|
use Streaming\Exception\InvalidArgumentException; |
17
|
|
|
use Streaming\Exception\RuntimeException; |
18
|
|
|
|
19
|
|
|
class GoogleCloudStorage implements CloudInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \Google\Cloud\Storage\Bucket $bucket |
23
|
|
|
*/ |
24
|
|
|
private $bucket; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* GoogleCloudStorage constructor. |
28
|
|
|
* @param string $bucket |
29
|
|
|
* @param bool $userProject |
30
|
|
|
* @param array $config |
31
|
|
|
*/ |
32
|
|
|
public function __construct(array $config, string $bucket, $userProject = false) |
33
|
|
|
{ |
34
|
|
|
try { |
35
|
|
|
$storage = new StorageClient($config); |
36
|
|
|
$this->bucket = $storage->bucket($bucket, $userProject); |
37
|
|
|
} catch (\Exception $e) { |
38
|
|
|
throw new InvalidArgumentException(sprintf("Invalid inputs:\n %s", $e->getMessage()), $e->getCode(), $e); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $dir |
44
|
|
|
* @param array $options |
45
|
|
|
*/ |
46
|
|
|
public function uploadDirectory(string $dir, array $options = []): void |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
|
|
foreach (scandir($dir) as $filename) { |
50
|
|
|
$path = $dir . DIRECTORY_SEPARATOR . $filename; |
51
|
|
|
|
52
|
|
|
if (is_file($path)) { |
53
|
|
|
$this->bucket->upload(fopen($path, 'r'), $options); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} catch (\Exception $e) { |
57
|
|
|
throw new RuntimeException(sprintf("There was an error during uploading files:\n %s", $e->getMessage()), $e->getCode(), $e); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $save_to |
63
|
|
|
* @param array $options |
64
|
|
|
*/ |
65
|
|
|
public function download(string $save_to, array $options): void |
66
|
|
|
{ |
67
|
|
|
$name = $options['name']; |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$this->bucket->object($name) |
71
|
|
|
->downloadToFile($save_to); |
72
|
|
|
} catch (\Exception $e) { |
73
|
|
|
throw new RuntimeException(sprintf("There was an error during fetch the file:\n %s", $e->getMessage()), $e->getCode(), $e); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |