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 Streaming\Exception\InvalidArgumentException; |
17
|
|
|
use Streaming\Exception\RuntimeException; |
18
|
|
|
use Streaming\File; |
19
|
|
|
|
20
|
|
|
class MicrosoftAzure implements CloudInterface |
21
|
|
|
{ |
22
|
|
|
/** @var \MicrosoftAzure\Storage\Blob\BlobRestProxy*/ |
23
|
|
|
private $blobClient; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* MicrosoftAzure constructor. |
27
|
|
|
* @param $connection |
28
|
|
|
* @param array $options |
29
|
|
|
*/ |
30
|
|
|
public function __construct($connection, array $options = []) |
31
|
|
|
{ |
32
|
|
|
if(!class_exists('\MicrosoftAzure\Storage\Blob\BlobRestProxy')){ |
33
|
|
|
throw new RuntimeException('MicrosoftAzure\Storage\Blob\BlobRestProxy not found. make sure the package is installed: composer require microsoft/azure-storage-blob'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->blobClient = \MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlobService($connection, $options); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Upload a entire directory to a cloud |
41
|
|
|
* @param string $dir |
42
|
|
|
* @param array $options |
43
|
|
|
*/ |
44
|
|
|
public function uploadDirectory(string $dir, array $options): void |
45
|
|
|
{ |
46
|
|
|
if(!isset($options['container'])){ |
47
|
|
|
throw new InvalidArgumentException("You should set the container in the array"); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
foreach (scandir($dir) as $filename) { |
52
|
|
|
$path = $dir . DIRECTORY_SEPARATOR . $filename; |
53
|
|
|
|
54
|
|
|
if (is_file($path)) { |
55
|
|
|
$this->blobClient->createBlockBlob($options['container'], $filename, fopen($path, "r"), $options['CreateBlockBlobOptions'] ?? null); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} catch (\MicrosoftAzure\Storage\Common\Exceptions\ServiceException $e) { |
59
|
|
|
throw new RuntimeException(sprintf("An error occurred while uploading files: %s", $e->getMessage()), $e->getCode(), $e); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Download a file from a cloud |
65
|
|
|
* @param string $save_to |
66
|
|
|
* @param array $options |
67
|
|
|
*/ |
68
|
|
|
public function download(string $save_to, array $options): void |
69
|
|
|
{ |
70
|
|
|
if(!isset($options['container']) || !isset($options['blob'])){ |
71
|
|
|
throw new InvalidArgumentException("You should set the container and blob in the array"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$blob = $this->blobClient->getBlob($options['container'], $options['blob']); |
76
|
|
|
File::put($save_to, $blob->getContentStream()); |
77
|
|
|
} catch (\MicrosoftAzure\Storage\Common\Exceptions\ServiceException $e) { |
78
|
|
|
throw new RuntimeException(sprintf("An error occurred while downloading the file: %s", $e->getMessage()), $e->getCode(), $e); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |