Passed
Push — master ( 63c9ea...e1ec1e )
by Amin
03:49
created

AWS::downloadFile()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
nc 6
nop 3
dl 0
loc 16
rs 9.9
c 1
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;
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";
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? $resu...URL'] : 'It is private' could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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
}