Completed
Push — master ( baa637...9b813d )
by Amin
04:08 queued 11s
created

AWS::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 Aws\S3\Exception\S3Exception;
17
use Aws\S3\S3Client;
18
use Aws\S3\Transfer;
19
use Streaming\Exception\Exception;
20
use Streaming\Exception\RuntimeException;
21
22
class AWS implements CloudInterface
23
{
24
    private $s3;
25
26
    /**
27
     * AWS constructor.
28
     * @param $config
29
     */
30
    public function __construct(array $config)
31
    {
32
        $this->s3 = new S3Client($config);;
33
    }
34
35
    /**
36
     * @param string $save_to
37
     * @param array $options
38
     * @throws Exception
39
     */
40
    public function download(string $save_to, array $options): void
41
    {
42
        $bucket = $options['bucket'];
43
        $key = $options['key'];
44
45
        try {
46
            $file = $this->s3->getObject([
47
                'Bucket' => $bucket,
48
                'Key' => $key
49
            ]);
50
51
            if ($file['ContentLength'] > 0 && !empty($file['ContentType'])) {
52
                $body = $file->get('Body');
53
                file_put_contents($save_to, $body);
54
            } else {
55
                throw new Exception("There is no file in the bucket");
56
            }
57
        } catch (S3Exception $e) {
58
            throw new RuntimeException("There was an error downloading the file.\n error: " . $e->getMessage(), $e->getCode(), $e);
59
        }
60
    }
61
62
    /**
63
     * @param string $dir
64
     * @param array $options
65
     */
66
    public function uploadDirectory(string $dir, array $options): void
67
    {
68
        $dest = $options['dest'];
69
70
        try {
71
            $manager = new Transfer($this->s3, $dir, $dest);
72
            $manager->transfer();
73
        } catch (S3Exception $e) {
74
            throw new RuntimeException("There was an error downloading the file.\n error: " . $e->getMessage(), $e->getCode(), $e);
75
        }
76
    }
77
}