1
|
|
|
<?php namespace AwsHelper; |
2
|
|
|
|
3
|
|
|
use Aws\S3\S3Client; |
4
|
|
|
use Aws\S3\Exception\S3Exception; |
5
|
|
|
|
6
|
|
|
class S3Helper |
7
|
|
|
{ |
8
|
|
|
protected $s3; |
9
|
|
|
protected $adapter; |
10
|
|
|
protected $bucket_name; |
11
|
|
|
protected $region; |
12
|
|
|
|
13
|
|
|
public function __construct(AwsHelper $adapter, $bucket_name, $region) |
14
|
|
|
{ |
15
|
|
|
$this->bucket_name = $bucket_name; |
16
|
|
|
$this->adapter = $adapter; |
17
|
|
|
$this->region = $region; |
18
|
|
|
$this->connect(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function connect() |
22
|
|
|
{ |
23
|
|
|
$this->s3 = S3Client::factory($this->adapter->getDefaultOptions() + ['region' => $this->region]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/* When accessing the S3Client you should call this function everytime |
27
|
|
|
so that you can be sure that the token isn't going to expire on you in the |
28
|
|
|
middle of a call */ |
29
|
|
|
public function getClient() |
30
|
|
|
{ |
31
|
|
|
$expirationTime = $this->adapter->getJSON()->Expiration; |
32
|
|
|
if ($this->adapter->hasAccessExpired($expirationTime)) { |
33
|
|
|
$this->connect(); |
34
|
|
|
} |
35
|
|
|
return $this->s3; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/* |
39
|
|
|
@param $file_location The location of the file on your local system |
40
|
|
|
@param $s3_location The location of the file you want to upload to s3 |
41
|
|
|
@param $acl What access the file should have, options are: private, |
42
|
|
|
public-read, public-read-write, authenticated-read, bucket-owner-read, |
43
|
|
|
bucket-owner-full-control |
44
|
|
|
@param $params Additional parameters you want to pass to S3 client. |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function saveFile($file_location, $s3_location, $acl = 'private', $params = []) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
if ($this->s3 == null) |
49
|
|
|
return false; |
50
|
|
|
|
51
|
|
|
$this->getClient(); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$result = $this->s3->putObject([ |
|
|
|
|
55
|
|
|
'Bucket' => $this->bucket_name, |
56
|
|
|
'Key' => $s3_location, |
57
|
|
|
'SourceFile' => $file_location, |
58
|
|
|
'ACL' => $acl, |
59
|
|
|
] + $params); |
60
|
|
|
} catch (\Exception $e) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/* |
66
|
|
|
@param $file_content The file content to upload to s3 |
67
|
|
|
@param $s3_location The location of the file you want to upload to s3 |
68
|
|
|
@param $acl What access the file should have, options are: private, |
69
|
|
|
public-read, public-read-write, authenticated-read, bucket-owner-read, |
70
|
|
|
bucket-owner-full-control |
71
|
|
|
@param $params Additional parameters you want to pass to S3 client. |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function saveFileContent($file_content, $s3_location, $acl = 'private', $params = []) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
if ($this->s3 == null) |
76
|
|
|
return false; |
77
|
|
|
|
78
|
|
|
$this->getClient(); |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$result = $this->s3->putObject([ |
|
|
|
|
82
|
|
|
'Bucket' => $this->bucket_name, |
83
|
|
|
'Key' => $s3_location, |
84
|
|
|
'Body' => $file_content, |
85
|
|
|
'ACL' => $acl, |
86
|
|
|
] + $params); |
87
|
|
|
} catch (\Exception $e) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* |
93
|
|
|
@param $s3_location the location of the file on s3 |
94
|
|
|
@return A pointer to a temporary file that is deleted upon script exit |
95
|
|
|
or false if error |
96
|
|
|
*/ |
97
|
|
|
public function getFile($s3_location) |
98
|
|
|
{ |
99
|
|
|
if ($this->s3 == null) |
100
|
|
|
return false; |
101
|
|
|
|
102
|
|
|
$this->getClient(); |
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
$result = $this->s3->getObject([ |
106
|
|
|
'Bucket' => $this->bucket_name, |
107
|
|
|
'Key' => $s3_location, |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
catch (\Exception $e) |
111
|
|
|
{ |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$result['Body']->rewind(); |
116
|
|
|
$temp_file = tmpfile(); |
117
|
|
|
while ($data = $result['Body']->read(1024)) { |
118
|
|
|
fwrite($temp_file, $data); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
fseek($temp_file, 0); |
122
|
|
|
return $temp_file; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.