|
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; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
use Aws\S3\Exception\S3Exception; |
|
17
|
|
|
use Aws\S3\S3Client; |
|
18
|
|
|
use Aws\S3\Transfer; |
|
19
|
|
|
use Streaming\Exception\Exception; |
|
20
|
|
|
|
|
21
|
|
|
class AWS |
|
22
|
|
|
{ |
|
23
|
|
|
private $s3; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* AWS constructor. |
|
27
|
|
|
* @param $config |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(array $config) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->s3 = new S3Client($config);; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $bucket |
|
36
|
|
|
* @param $key |
|
37
|
|
|
* @param $filename |
|
38
|
|
|
* @param string $acl |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
* @throws Exception |
|
41
|
|
|
*/ |
|
42
|
|
|
public function uploadFile(string $bucket, string $key, string $filename, string $acl = 'public-read'): string |
|
43
|
|
|
{ |
|
44
|
|
|
try { |
|
45
|
|
|
$result = $this->s3->putObject([ |
|
46
|
|
|
'Bucket' => $bucket, |
|
47
|
|
|
'Key' => $key, |
|
48
|
|
|
'Body' => fopen($filename, 'r'), |
|
49
|
|
|
'ACL' => $acl, |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
return isset($result['ObjectURL']) ? $result['ObjectURL'] : "It is private"; |
|
|
|
|
|
|
53
|
|
|
} catch (S3Exception $e) { |
|
54
|
|
|
throw new Exception("There was an error uploading the file.\n error: " . $e->getMessage()); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $bucket |
|
60
|
|
|
* @param $key |
|
61
|
|
|
* @param $filename |
|
62
|
|
|
* @throws Exception |
|
63
|
|
|
*/ |
|
64
|
|
|
public function downloadFile(string $bucket, string $key, string $filename): void |
|
65
|
|
|
{ |
|
66
|
|
|
try { |
|
67
|
|
|
$file = $this->s3->getObject([ |
|
68
|
|
|
'Bucket' => $bucket, |
|
69
|
|
|
'Key' => $key |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
if ($file['ContentLength'] > 0 && !empty($file['ContentType'])) { |
|
73
|
|
|
$body = $file->get('Body'); |
|
74
|
|
|
file_put_contents($filename, $body); |
|
75
|
|
|
} else { |
|
76
|
|
|
throw new Exception("There is no file in the bucket"); |
|
77
|
|
|
} |
|
78
|
|
|
} catch (S3Exception $e) { |
|
79
|
|
|
throw new Exception("There was an error downloading the file.\n error: " . $e->getMessage()); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param $source |
|
85
|
|
|
* @param $dest |
|
86
|
|
|
*/ |
|
87
|
|
|
public function uploadAndDownloadDirectory(string $source, string $dest): void |
|
88
|
|
|
{ |
|
89
|
|
|
$manager = new Transfer($this->s3, $source, $dest); |
|
90
|
|
|
$manager->transfer(); |
|
91
|
|
|
} |
|
92
|
|
|
} |