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 S3 implements CloudInterface |
21
|
|
|
{ |
22
|
|
|
/** @var \Aws\S3\S3Client*/ |
23
|
|
|
private $s3; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* AWS constructor. |
27
|
|
|
* @param $config |
28
|
|
|
*/ |
29
|
|
|
public function __construct(array $config) |
30
|
|
|
{ |
31
|
|
|
if(!class_exists('\Aws\S3\S3Client')){ |
32
|
|
|
throw new RuntimeException('Aws\S3\S3Client not found. make sure the package is installed: composer require aws/aws-sdk-php'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->s3 = new \Aws\S3\S3Client($config);; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $dir |
40
|
|
|
* @param array $options |
41
|
|
|
*/ |
42
|
|
|
public function uploadDirectory(string $dir, array $options): void |
43
|
|
|
{ |
44
|
|
|
if(!isset($options['dest'])){ |
45
|
|
|
throw new InvalidArgumentException("You should set the dest in the array"); |
46
|
|
|
} |
47
|
|
|
$dest = $options['dest']; |
48
|
|
|
unset($options['dest']); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
(new \Aws\S3\Transfer($this->s3, $dir, $dest, $options))->transfer(); |
52
|
|
|
} catch (\Aws\S3\Exception\S3Exception $e) { |
53
|
|
|
throw new RuntimeException("An error occurred while uploading files: " . $e->getMessage(), $e->getCode(), $e); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $save_to |
59
|
|
|
* @param array $options |
60
|
|
|
* @throws RuntimeException |
61
|
|
|
*/ |
62
|
|
|
public function download(string $save_to, array $options): void |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
|
|
$file = $this->s3->getObject($options); |
66
|
|
|
if ($file['ContentLength'] > 0 && !empty($file['ContentType'])) { |
67
|
|
|
File::put($save_to, $file->get('Body')); |
68
|
|
|
} else { |
69
|
|
|
throw new RuntimeException("File not found!"); |
70
|
|
|
} |
71
|
|
|
} catch (\Aws\S3\Exception\S3Exception $e) { |
72
|
|
|
throw new RuntimeException("An error occurred while downloading the file: " . $e->getMessage(), $e->getCode(), $e); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |