1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use Aws\S3\S3Client; |
5
|
|
|
use phpbu\App\Result; |
6
|
|
|
use phpbu\App\Backup\Sync; |
7
|
|
|
use phpbu\App\Backup\Target; |
8
|
|
|
use phpbu\App\Util\Arr; |
9
|
|
|
use phpbu\App\Util\Str; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Amazon S3 Sync |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @subpackage Backup |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
19
|
|
|
* @link http://phpbu.de/ |
20
|
|
|
* @since Class available since Release 1.1.4 |
21
|
|
|
*/ |
22
|
|
|
class AmazonS3v3 extends AmazonS3 |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Execute the sync. |
26
|
|
|
* |
27
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
28
|
|
|
* @param \phpbu\App\Backup\Target $target |
29
|
|
|
* @param \phpbu\App\Result $result |
30
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
31
|
|
|
*/ |
32
|
|
|
public function sync(Target $target, Result $result) |
33
|
|
|
{ |
34
|
|
|
$sourcePath = $target->getPathname(); |
35
|
|
|
$targetPath = $this->getUploadPath($target); |
36
|
|
|
|
37
|
|
|
$s3 = new S3Client([ |
38
|
|
|
'region' => $this->region, |
39
|
|
|
'version' => '2006-03-01', |
40
|
|
|
'credentials' => [ |
41
|
|
|
'key' => $this->key, |
42
|
|
|
'secret' => $this->secret, |
43
|
|
|
] |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
$s3->registerStreamWrapper(); |
48
|
|
|
$stream = fopen($targetPath, 'w'); |
49
|
|
|
$source = fopen($sourcePath, 'r'); |
50
|
|
|
while(!feof($source)) { |
51
|
|
|
fwrite($stream, fread($source, 4096)); |
52
|
|
|
} |
53
|
|
|
fclose($stream); |
54
|
|
|
|
55
|
|
|
} catch (\Exception $e) { |
56
|
|
|
throw new Exception($e->getMessage(), null, $e); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$result->debug('upload: done'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the s3 upload path |
64
|
|
|
* |
65
|
|
|
* @param \phpbu\App\Backup\Target $target |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getUploadPath(Target $target) |
69
|
|
|
{ |
70
|
|
|
return 's3://' . $this->bucket |
71
|
|
|
. (substr($this->path, 0, 1) == '/' ? '' : '/') |
72
|
|
|
. $this->path |
73
|
|
|
. (substr($this->path, -1, 1) == '/' ? '' : '/') |
74
|
|
|
. $target->getFilename(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|