This repository was archived by the owner on May 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathS3ClientComponent.php
311 lines (284 loc) · 9.44 KB
/
S3ClientComponent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
/**
* CakePHP3 S3Client Component
* with
* AWS SDK for PHP3
* Created by rito
* @see https://github.com/rito328/cakephp-s3-aws-sdk-for-php
* @see https://www.ritolab.com/entry/104
* @see https://aws.amazon.com/jp/sdk-for-php/
* @see https://github.com/aws/aws-sdk-php
*/
class S3ClientComponent extends Component
{
protected $_defaultConfig = [];
protected $default_bucket;
public function initialize(array $config)
{
$this->s3 = S3Client::factory([
'credentials' => [
'key' => env('AWS_S3_KEY', ''),
'secret' => env('AWS_S3_SECRET', ''),
],
'region' => env('AWS_S3_REGION', ''),
'version' => 'latest',
]);
$this->default_bucket = env('AWS_S3_BUCKET', '');
}
/**
* Get a list of files
* @param string $bucket_name
* @param string $dir
* @param int $get_max
* @return array
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.S3Client.html#_listObjects
*/
public function getList($bucket_name=null, $dir=null, $get_max=100)
{
try {
if(!$bucket_name) $bucket_name = $this->default_bucket;
$list_obj = $this->s3->listObjects([
'Bucket' => $bucket_name,
'MaxKeys' => $get_max,
'Prefix' => $dir
]);
foreach ($list_obj['Contents'] as $file) {
if (mb_substr($file['Key'], -1) !== "/" && (!$dir || ($dir && strpos($file['Key'], sprintf('%s/', $dir)) !== false))) {
$result[] = $file['Key'];
}
}
return $result;
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
/**
* Uploading files
* @param string $file_path
* @param string $store_path
* @param string $bucket_name
* @return mixed
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putobject
*/
public function putFile($file_path, $store_path, $bucket_name=null)
{
try {
if(!$bucket_name) $bucket_name = $this->default_bucket;
$result = $this->s3->putObject(array(
'Bucket' => $bucket_name,
'Key' => $store_path,
'SourceFile' => $file_path,
));
return $result;
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
/**
* File download
* @param string $s3_file_path
* @param string $store_dir_path
* @param string $bucket_name
* @return mixed
* @see https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/RetrieveObjSingleOpPHP.html
*/
public function getFile($s3_file_path, $store_file_path, $bucket_name=null)
{
try {
if(!$bucket_name) $bucket_name = $this->default_bucket;
$result = $this->s3->getObject([
'Bucket' => $bucket_name,
'Key' => $s3_file_path,
'SaveAs' => $store_file_path
]);
return $result;
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
/**
* Directory download (Recursive download)
* @param string $s3_dir_path
* @param string $local_dir_path
* @param string $bucket_name
*/
public function getDirectory($s3_dir_path, $local_dir_path, $bucket_name=null)
{
try {
if(!$bucket_name) $bucket_name = $this->default_bucket;
$file_list = $this->getList($bucket_name, $s3_dir_path);
$this->chkDirHandle($file_list, $local_dir_path);
foreach ($file_list as $from_path) {
$to_path = sprintf('%s/%s', $local_dir_path, $from_path);
$this->getFile($from_path, $to_path);
}
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
/**
* Copying files
* @param string $s3_file_path
* @param string $s3_copy_file_path
* @param string $bucket_name_from
* @param string $bucket_name_to
* @return mixed
* @see https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/CopyingObjectUsingPHP.html
*/
public function copyFile($s3_file_path, $s3_copy_file_path, $bucket_name_from=null, $bucket_name_to=null)
{
try {
if(!$bucket_name_from) $bucket_name_from = $this->default_bucket;
if(!$bucket_name_to) $bucket_name_to = $this->default_bucket;
$result = $this->s3->copyObject(array(
'Bucket' => $bucket_name_to,
'Key' => $s3_copy_file_path,
'CopySource' => sprintf('%s/%s', $bucket_name_from, $s3_file_path),
));
return $result;
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
/**
* Copy directory (Recursive copy)
* @param string $s3_from_dir
* @param string $s3_to_dir
* @param string $bucket_name_from
* @param string $bucket_name_to
*/
public function copyDirectory($s3_from_dir, $s3_to_dir, $bucket_name_from=null, $bucket_name_to=null)
{
try {
if(!$bucket_name_from) $bucket_name_from = $this->default_bucket;
if(!$bucket_name_to) $bucket_name_to = $this->default_bucket;
$file_list = $this->getList($bucket_name_from, $s3_from_dir);
foreach ($file_list as $from_path) {
$to_path = sprintf('%s/%s', $s3_to_dir, basename($from_path));
$this->copyFile($from_path, $to_path);
}
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
/**
* Moving files
* @param $s3_from_path
* @param $s3_to_path
* @return bool|mixed
*/
public function moveFile($s3_from_path, $s3_to_path)
{
try {
$result = false;
if($this->copyFile($s3_from_path, $s3_to_path)) {
$result = $this->deleteFile($s3_from_path);
}
return $result;
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
/**
* Move directory (Recursive movement)
* @param string $s3_from_dir
* @param string $s3_to_dir
* @param string null $bucket_name
*/
public function moveDirectory($s3_from_dir, $s3_to_dir, $bucket_name=null)
{
try {
if(!$bucket_name) $bucket_name = $this->default_bucket;
$file_list = $this->getList($bucket_name, $s3_from_dir);
foreach ($file_list as $from_path) {
$to_path = sprintf('%s/%s', $s3_to_dir, basename($from_path));
$this->moveFile($from_path, $to_path);
}
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
/**
* Delete files
* @param string $file_path
* @param string $bucket_name
* @return mixed
* @see https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/DeletingMultipleObjectsUsingPHPSDK.html
*/
public function deleteFile($file_path, $bucket_name=null)
{
try {
if(!$bucket_name) $bucket_name = $this->default_bucket;
$result = $this->s3->deleteObject(array(
'Bucket' => $bucket_name,
'Key' => $file_path
));
return $result;
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
/**
* Delete directory (Remove recursively)
* @param string $dir_name
* @param string $bucket_name
* @return mixed
* @see https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/DeletingMultipleObjectsUsingPHPSDK.html
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#deleteobjects
*/
public function deleteDirectory($dir_name, $bucket_name=null)
{
try {
if(!$bucket_name) $bucket_name = $this->default_bucket;
$file_list = $this->getList($bucket_name, $dir_name);
$files = $this->createArrayMultipleObjects($file_list);
$result = $this->s3->deleteObjects(array(
'Bucket' => $bucket_name,
'Delete' => [
'Objects' => $files
]
));
return $result;
} catch (S3Exception $e) {
echo $e->getMessage();
}
}
/**
* Convert from file list to array for multiple objects
* @param $file_list
* @return array
*/
private function createArrayMultipleObjects($file_list)
{
foreach ($file_list as $name) {
$files[] = array('Key' => $name);
}
return $files;
}
/**
* Recursively check the path directories in the file list
* @param $file_list
*/
private function chkDirHandle($file_list, $local_dir_path)
{
foreach ($file_list as $filename) {
$local_path = sprintf('%s/%s', $local_dir_path, dirname($filename));
$this->chkDir($local_path);
}
}
/**
* Confirm existence of directory and create it if it does not exist.
* @param $local_dir_path
*/
private function chkDir($local_dir_path)
{
if(!file_exists($local_dir_path)) {
mkdir($local_dir_path, 0777, true);
}
}
}