1 | <?php |
||
22 | class AmazonS3v3 extends AmazonS3 |
||
23 | { |
||
24 | use Cleanable; |
||
25 | |||
26 | /** |
||
27 | * Amazon S3 client. |
||
28 | * |
||
29 | * @var S3Client; |
||
30 | */ |
||
31 | protected $client; |
||
32 | |||
33 | /** |
||
34 | * Configure the sync. |
||
35 | * |
||
36 | * @see \phpbu\App\Backup\Sync::setup() |
||
37 | * @param array $config |
||
38 | * @throws \phpbu\App\Backup\Sync\Exception |
||
39 | * @throws \phpbu\App\Exception |
||
40 | */ |
||
41 | 9 | public function setup(array $config) |
|
42 | { |
||
43 | 9 | parent::setup($config); |
|
44 | |||
45 | 4 | $this->setUpCleanable($config); |
|
46 | 4 | } |
|
47 | |||
48 | /** |
||
49 | * Execute the sync. |
||
50 | * |
||
51 | * @see \phpbu\App\Backup\Sync::sync() |
||
52 | * @param \phpbu\App\Backup\Target $target |
||
53 | * @param \phpbu\App\Result $result |
||
54 | * @throws \phpbu\App\Backup\Sync\Exception |
||
55 | */ |
||
56 | public function sync(Target $target, Result $result) |
||
57 | { |
||
58 | $this->client = new S3Client([ |
||
59 | 'region' => $this->region, |
||
60 | 'version' => '2006-03-01', |
||
61 | 'credentials' => [ |
||
62 | 'key' => $this->key, |
||
63 | 'secret' => $this->secret, |
||
64 | ] |
||
65 | ]); |
||
66 | |||
67 | if (!$this->client->doesBucketExist($this->bucket)) { |
||
68 | $result->debug('create s3 bucket'); |
||
69 | $this->createBucket($this->client); |
||
70 | } |
||
71 | |||
72 | try { |
||
73 | $this->upload($target, $this->client); |
||
74 | } catch (\Exception $e) { |
||
75 | throw new Exception($e->getMessage(), null, $e); |
||
76 | } |
||
77 | // run remote cleanup |
||
78 | $this->cleanup($target, $result); |
||
79 | $result->debug('upload: done'); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Creates collector for Amazon S3 |
||
84 | * |
||
85 | * @param \phpbu\App\Backup\Target $target |
||
86 | * @return \phpbu\App\Backup\Collector |
||
87 | */ |
||
88 | protected function createCollector(Target $target) : Collector |
||
89 | { |
||
90 | $path = new Path($this->pathRaw, $this->time); |
||
91 | return new Collector\AmazonS3v3($target, $path, $this->client, $this->bucket); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Simulate the sync execution. |
||
96 | * |
||
97 | * @param \phpbu\App\Backup\Target $target |
||
98 | * @param \phpbu\App\Result $result |
||
99 | */ |
||
100 | 1 | public function simulate(Target $target, Result $result) |
|
101 | { |
||
102 | 1 | parent::simulate($target, $result); |
|
103 | |||
104 | 1 | $this->simulateRemoteCleanup($target, $result); |
|
105 | 1 | } |
|
106 | |||
107 | /** |
||
108 | * Create a s3 bucket. |
||
109 | * |
||
110 | * @param \Aws\S3\S3Client $s3 |
||
111 | */ |
||
112 | private function createBucket(S3Client $s3) |
||
113 | { |
||
114 | $s3->createBucket([ |
||
115 | 'ACL' => $this->acl, |
||
116 | 'Bucket' => $this->bucket, |
||
117 | 'CreateBucketConfiguration' => [ |
||
118 | 'LocationConstraint' => $this->region, |
||
119 | ] |
||
120 | ]); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Upload backup to Amazon S3 bucket. |
||
125 | * |
||
126 | * @param \phpbu\App\Backup\Target $target |
||
127 | * @param \Aws\S3\S3Client $s3 |
||
128 | * @throws \phpbu\App\Backup\Sync\Exception |
||
129 | * @throws \phpbu\App\Exception |
||
130 | */ |
||
131 | private function upload(Target $target, S3Client $s3) |
||
139 | |||
140 | /** |
||
141 | * Upload via stream wrapper. |
||
142 | * |
||
143 | * @param \phpbu\App\Backup\Target $target |
||
144 | * @param \Aws\S3\S3Client $s3 |
||
145 | * @throws \phpbu\App\Backup\Sync\Exception |
||
146 | */ |
||
147 | private function uploadStream(Target $target, S3Client $s3) |
||
157 | |||
158 | /** |
||
159 | * Upload via multi part. |
||
160 | * |
||
161 | * @param \phpbu\App\Backup\Target $target |
||
162 | * @param \Aws\S3\S3Client $s3 |
||
163 | * @param \Aws\Exception\MultipartUploadException |
||
164 | */ |
||
165 | private function uploadMultiPart(Target $target, S3Client $s3) |
||
173 | |||
174 | /** |
||
175 | * Open stream and validate it. |
||
176 | * |
||
177 | * @param string $path |
||
178 | * @param string $mode |
||
179 | * @return resource |
||
180 | * @throws \phpbu\App\Backup\Sync\Exception |
||
181 | */ |
||
182 | private function getFileHandle($path, $mode) |
||
190 | |||
191 | /** |
||
192 | * Get the s3 upload path |
||
193 | * |
||
194 | * @param \phpbu\App\Backup\Target $target |
||
195 | * @return string |
||
196 | */ |
||
197 | 2 | public function getUploadPath(Target $target) |
|
201 | } |
||
202 |