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 base class |
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 3.0.0 |
21
|
|
|
*/ |
22
|
|
|
abstract class AmazonS3 implements Simulator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* AWS key |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $key; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* AWS secret |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $secret; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* AWS S3 bucket |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $bucket; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* AWS S3 region |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $region; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* AWS remote path / object key |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $path; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* AWS acl |
61
|
|
|
* 'private' by default |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $acl; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Use multi part config |
69
|
|
|
* |
70
|
|
|
* @var boolean |
71
|
|
|
*/ |
72
|
|
|
protected $multiPartUpload; |
73
|
|
|
|
74
|
6 |
|
/** |
75
|
|
|
* Min multi part upload size |
76
|
6 |
|
* |
77
|
|
|
* @var int |
78
|
|
|
*/ |
79
|
6 |
|
protected $minMultiPartUploadSize = 5242880; |
80
|
1 |
|
|
81
|
|
|
/** |
82
|
5 |
|
* Max stream upload size |
83
|
1 |
|
* |
84
|
|
|
* @var int |
85
|
4 |
|
*/ |
86
|
1 |
|
protected $maxStreamUploadSize = 104857600; |
87
|
|
|
|
88
|
3 |
|
/** |
89
|
1 |
|
* Configure the sync. |
90
|
|
|
* |
91
|
2 |
|
* @see \phpbu\App\Backup\Sync::setup() |
92
|
1 |
|
* @param array $config |
93
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
94
|
1 |
|
*/ |
95
|
1 |
|
public function setup(array $config) |
96
|
1 |
|
{ |
97
|
1 |
|
if (!class_exists('\\Aws\\S3\\S3Client')) { |
98
|
1 |
|
throw new Exception('Amazon SDK not loaded: use composer to install "aws/aws-sdk-php"'); |
99
|
1 |
|
} |
100
|
1 |
|
|
101
|
|
|
// check for mandatory options |
102
|
|
View Code Duplication |
foreach (['key', 'secret', 'bucket', 'region', 'path'] as $option) { |
|
|
|
|
103
|
|
|
if (!Arr::isSetAndNotEmptyString($config, $option)) { |
104
|
|
|
throw new Exception('AWS S3 ' . $option . ' is mandatory'); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->key = $config['key']; |
109
|
|
|
$this->secret = $config['secret']; |
110
|
|
|
$this->bucket = $config['bucket']; |
111
|
|
|
$this->region = $config['region']; |
112
|
|
|
$this->path = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path'])); |
113
|
|
|
$this->acl = Arr::getValue($config, 'acl', 'private'); |
114
|
|
|
$this->multiPartUpload = Str::toBoolean(Arr::getValue($config, 'useMultiPartUpload'), false); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Execute the sync |
119
|
|
|
* |
120
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
121
|
|
|
* @param \phpbu\App\Backup\Target $target |
122
|
|
|
* @param \phpbu\App\Result $result |
123
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
124
|
|
|
*/ |
125
|
|
|
abstract public function sync(Target $target, Result $result); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Simulate the sync execution. |
129
|
|
|
* |
130
|
|
|
* @param \phpbu\App\Backup\Target $target |
131
|
|
|
* @param \phpbu\App\Result $result |
132
|
|
|
*/ |
133
|
|
|
public function simulate(Target $target, Result $result) |
134
|
|
|
{ |
135
|
|
|
$result->debug( |
136
|
|
|
'sync backup to Amazon S3' . PHP_EOL |
137
|
|
|
. ' region: ' . $this->region . PHP_EOL |
138
|
|
|
. ' key: ' . $this->key . PHP_EOL |
139
|
|
|
. ' secret: ********' . PHP_EOL |
140
|
|
|
. ' location: ' . $this->bucket |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Should multi part upload be used. |
146
|
|
|
* |
147
|
|
|
* @param \phpbu\App\Backup\Target $target |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
protected function useMultiPartUpload(Target $target) |
151
|
|
|
{ |
152
|
|
|
// files bigger 5GB have to be uploaded via multi part |
153
|
|
|
// files uploaded with multi part upload has to be at least 5MB |
154
|
|
|
return ( |
155
|
|
|
$target->getSize() > $this->maxStreamUploadSize || $this->multiPartUpload |
156
|
|
|
) && $target->getSize() > $this->minMultiPartUploadSize; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.