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
|
|
|
|
16
|
|
|
use MicrosoftAzure\Storage\Blob\BlobRestProxy; |
17
|
|
|
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException; |
18
|
|
|
use Streaming\Exception\RuntimeException; |
19
|
|
|
|
20
|
|
|
class MicrosoftAzure implements CloudInterface |
21
|
|
|
{ |
22
|
|
|
private $blobClient; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* MicrosoftAzure constructor. |
26
|
|
|
* @param $connectionString |
27
|
|
|
*/ |
28
|
|
|
public function __construct($connectionString) |
29
|
|
|
{ |
30
|
|
|
$this->blobClient = BlobRestProxy::createBlobService($connectionString); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Upload a entire directory to a cloud |
35
|
|
|
* @param string $dir |
36
|
|
|
* @param array $options |
37
|
|
|
*/ |
38
|
|
|
public function uploadDirectory(string $dir, array $options): void |
39
|
|
|
{ |
40
|
|
|
$container = $options['container']; |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
foreach (scandir($dir) as $filename) { |
44
|
|
|
$path = $dir . DIRECTORY_SEPARATOR . $filename; |
45
|
|
|
|
46
|
|
|
if (is_file($path)) { |
47
|
|
|
$this->blobClient->createBlockBlob($container, $filename, fopen($path, "r")); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} catch (ServiceException $e) { |
51
|
|
|
throw new RuntimeException(sprintf("There was an error during uploading files:\n %s", $e->getMessage()), $e->getCode(), $e); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Download a file from a cloud |
57
|
|
|
* @param string $save_to |
58
|
|
|
* @param array $options |
59
|
|
|
*/ |
60
|
|
|
public function download(string $save_to, array $options): void |
61
|
|
|
{ |
62
|
|
|
$container = $options['container']; |
63
|
|
|
$blob = $options['blob']; |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$getBlobResult = $this->blobClient->getBlob($container, $blob); |
67
|
|
|
} catch (ServiceException $e) { |
68
|
|
|
throw new RuntimeException(sprintf("There was an error during uploading files:\n %s", $e->getMessage()), $e->getCode(), $e); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
file_put_contents($save_to, $getBlobResult->getContentStream()); |
72
|
|
|
} |
73
|
|
|
} |