Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/S3/S3Transfer/AbstractMultipartUploader.php

raw symbols SCIP
1 <?php
2
3 namespace Aws\S3\S3Transfer;
4
5 use Aws\CommandInterface;
6 use Aws\CommandPool;
7 use Aws\ResultInterface;
8 use Aws\S3\S3ClientInterface;
9 use Aws\S3\S3Transfer\Models\S3TransferManagerConfig;
10 use Aws\S3\S3Transfer\Progress\AbstractTransferListener;
11 use Aws\S3\S3Transfer\Progress\TransferListenerNotifier;
12 use Aws\S3\S3Transfer\Progress\TransferProgressSnapshot;
13 use GuzzleHttp\Promise\Coroutine;
14 use GuzzleHttp\Promise\PromiseInterface;
15 use GuzzleHttp\Promise\PromisorInterface;
16 use Throwable;
17
18 /**
19 * Abstract base class for multipart operations
20 */
21 abstract class AbstractMultipartUploader implements PromisorInterface
22 {
23 public const PART_MIN_SIZE = 5 * 1024 * 1024; // 5 MiB
24 public const PART_MAX_SIZE = 5 * 1024 * 1024 * 1024; // 5 GiB
25 public const PART_MAX_NUM = 10000;
26
27 /** @var S3ClientInterface */
28 protected readonly S3ClientInterface $s3Client;
29
30 /** @var array */
31 protected readonly array $requestArgs;
32
33 /** @var array */
34 protected readonly array $config;
35
36 /** @var string|null */
37 protected string|null $uploadId;
38
39 /** @var array */
40 protected array $partsCompleted;
41
42 /** @var array */
43 protected array $onCompletionCallbacks = [];
44
45 /** @var TransferListenerNotifier|null */
46 protected ?TransferListenerNotifier $listenerNotifier;
47
48 /** Tracking Members */
49 /** @var TransferProgressSnapshot|null */
50 protected ?TransferProgressSnapshot $currentSnapshot;
51
52 /**
53 * @param S3ClientInterface $s3Client
54 * @param array $requestArgs
55 * @param array $config
56 * - target_part_size_bytes: (int, optional)
57 * - concurrency: (int, optional)
58 * @param string|null $uploadId
59 * @param array $partsCompleted
60 * @param TransferProgressSnapshot|null $currentSnapshot
61 * @param TransferListenerNotifier|null $listenerNotifier
62 */
63 public function __construct(
64 S3ClientInterface $s3Client,
65 array $requestArgs,
66 array $config = [],
67 ?string $uploadId = null,
68 array $partsCompleted = [],
69 ?TransferProgressSnapshot $currentSnapshot = null,
70 ?TransferListenerNotifier $listenerNotifier = null,
71 ) {
72 $this->s3Client = $s3Client;
73 $this->requestArgs = $requestArgs;
74 $this->validateConfig($config);
75 $this->config = $config;
76 $this->uploadId = $uploadId;
77 $this->partsCompleted = $partsCompleted;
78 $this->currentSnapshot = $currentSnapshot;
79 $this->listenerNotifier = $listenerNotifier;
80 }
81
82 /**
83 * @return PromiseInterface
84 */
85 abstract protected function createMultipartOperation(): PromiseInterface;
86
87 /**
88 * @return PromiseInterface
89 */
90 abstract protected function completeMultipartOperation(): PromiseInterface;
91
92 /**
93 * @return PromiseInterface
94 */
95 abstract protected function processMultipartOperation(): PromiseInterface;
96
97 /**
98 * @param int $partSize
99 * @param array $requestArgs
100 * @param array $partData
101 *
102 * @return void
103 */
104 abstract protected function partCompleted(
105 int $partSize,
106 array $requestArgs,
107 array $partData
108 ): void;
109
110 /**
111 * @return PromiseInterface
112 */
113 abstract protected function abortMultipartOperation(): PromiseInterface;
114
115 /**
116 * @return int
117 */
118 abstract protected function getTotalSize(): int;
119
120 /**
121 * @param ResultInterface $result
122 *
123 * @return mixed
124 */
125 abstract protected function createResponse(ResultInterface $result): mixed;
126
127 /**
128 * @param array $config
129 *
130 * @return void
131 */
132 protected function validateConfig(array &$config): void
133 {
134 if (!isset($config['target_part_size_bytes'])) {
135 $config['target_part_size_bytes'] = S3TransferManagerConfig::DEFAULT_TARGET_PART_SIZE_BYTES;
136 }
137
138 if (!isset($config['concurrency'])) {
139 $config['concurrency'] = S3TransferManagerConfig::DEFAULT_CONCURRENCY;
140 }
141
142 $partSize = $config['target_part_size_bytes'];
143 if ($partSize < self::PART_MIN_SIZE || $partSize > self::PART_MAX_SIZE) {
144 throw new \InvalidArgumentException(
145 "Part size config must be between " . self::PART_MIN_SIZE
146 ." and " . self::PART_MAX_SIZE . " bytes "
147 ."but it is configured to $partSize"
148 );
149 }
150 }
151
152 /**
153 * @return string|null
154 */
155 public function getUploadId(): ?string
156 {
157 return $this->uploadId;
158 }
159
160 /**
161 * @return array
162 */
163 public function getPartsCompleted(): array
164 {
165 return $this->partsCompleted;
166 }
167
168 /**
169 * Get the current progress snapshot.
170 * @return TransferProgressSnapshot|null
171 */
172 public function getCurrentSnapshot(): ?TransferProgressSnapshot
173 {
174 return $this->currentSnapshot;
175 }
176
177 /**
178 * @return PromiseInterface
179 */
180 public function promise(): PromiseInterface
181 {
182 return Coroutine::of(function () {
183 try {
184 yield $this->createMultipartOperation();
185 yield $this->processMultipartOperation();
186 yield $this->completeMultipartOperation();
187 } finally {
188 $this->callOnCompletionCallbacks();
189 }
190 })->then(function (ResultInterface $result) {
191 return $this->createResponse($result);
192 })->otherwise(function (Throwable $e) {
193 $this->operationFailed($e);
194
195 throw $e;
196 });
197 }
198
199 /**
200 * @return void
201 */
202 protected function sortParts(): void
203 {
204 usort($this->partsCompleted, function ($partOne, $partTwo) {
205 return $partOne['PartNumber']
206 <=> $partTwo['PartNumber'];
207 });
208 }
209
210 /**
211 * @param ResultInterface $result
212 * @param CommandInterface $command
213 *
214 * @return array
215 */
216 protected function collectPart(
217 ResultInterface $result,
218 CommandInterface $command
219 ): array
220 {
221 $checksumResult = match($command->getName()) {
222 'UploadPart' => $result,
223 'UploadPartCopy' => $result['CopyPartResult'],
224 default => $result[$command->getName() . 'Result']
225 };
226
227 $partNumber = $command['PartNumber'];
228 $partData = [
229 'PartNumber' => $partNumber,
230 'ETag' => $checksumResult['ETag'],
231 ];
232
233 if (isset($command['ChecksumAlgorithm'])) {
234 $checksumMemberName = 'Checksum' . strtoupper($command['ChecksumAlgorithm']);
235 $partData[$checksumMemberName] = $checksumResult[$checksumMemberName] ?? null;
236 }
237
238 $this->partsCompleted[$partNumber] = $partData;
239
240 return $partData;
241 }
242
243 /**
244 * @param \Iterator $commands
245 * @param callable $fulfilledCallback
246 * @param callable $rejectedCallback
247 * @return PromiseInterface
248 */
249 protected function createCommandPool(
250 \Iterator $commands,
251 callable $fulfilledCallback,
252 callable $rejectedCallback
253 ): PromiseInterface
254 {
255 return (new CommandPool(
256 $this->s3Client,
257 $commands,
258 [
259 'concurrency' => $this->config['concurrency'],
260 'fulfilled' => $fulfilledCallback,
261 'rejected' => $rejectedCallback
262 ]
263 ))->promise();
264 }
265
266 /**
267 * @param array $requestArgs
268 * @return void
269 */
270 protected function operationInitiated(array $requestArgs): void
271 {
272 if ($this->currentSnapshot === null) {
273 $this->currentSnapshot = new TransferProgressSnapshot(
274 $requestArgs['Key'],
275 0,
276 $this->getTotalSize()
277 );
278 }
279
280 $this->listenerNotifier?->transferInitiated([
281 AbstractTransferListener::REQUEST_ARGS_KEY => $requestArgs,
282 AbstractTransferListener::PROGRESS_SNAPSHOT_KEY => $this->currentSnapshot
283 ]);
284 }
285
286 /**
287 * @param ResultInterface $result
288 * @return void
289 */
290 protected function operationCompleted(ResultInterface $result): void
291 {
292 $newSnapshot = new TransferProgressSnapshot(
293 $this->currentSnapshot->getIdentifier(),
294 $this->currentSnapshot->getTransferredBytes(),
295 $this->currentSnapshot->getTotalBytes(),
296 $result->toArray(),
297 $this->currentSnapshot->getReason(),
298 );
299
300 $this->currentSnapshot = $newSnapshot;
301
302 $this->listenerNotifier?->transferComplete([
303 AbstractTransferListener::REQUEST_ARGS_KEY =>
304 $this->requestArgs,
305 AbstractTransferListener::PROGRESS_SNAPSHOT_KEY => $this->currentSnapshot
306 ]);
307 }
308
309 /**
310 * @param Throwable $reason
311 * @return void
312 *
313 */
314 protected function operationFailed(Throwable $reason): void
315 {
316 // Event already propagated
317 if ($this->currentSnapshot?->getReason() !== null) {
318 return;
319 }
320
321 if ($this->currentSnapshot === null) {
322 $this->currentSnapshot = new TransferProgressSnapshot(
323 'Unknown',
324 0,
325 0,
326 );
327 }
328
329 $this->currentSnapshot = new TransferProgressSnapshot(
330 $this->currentSnapshot->getIdentifier(),
331 $this->currentSnapshot->getTransferredBytes(),
332 $this->currentSnapshot->getTotalBytes(),
333 $this->currentSnapshot->getResponse(),
334 $reason
335 );
336
337 if (!empty($this->uploadId)) {
338 trigger_error(
339 "Multipart Upload with id: " . $this->uploadId . " failed",
340 );
341 $this->abortMultipartOperation()->wait();
342 }
343
344 $this->listenerNotifier?->transferFail([
345 AbstractTransferListener::REQUEST_ARGS_KEY =>
346 $this->requestArgs,
347 AbstractTransferListener::PROGRESS_SNAPSHOT_KEY => $this->currentSnapshot,
348 'reason' => $reason,
349 ]);
350 }
351
352 /**
353 * @return void
354 */
355 protected function callOnCompletionCallbacks(): void
356 {
357 foreach ($this->onCompletionCallbacks as $fn) {
358 if (is_callable($fn)) {
359 call_user_func($fn);
360 }
361 }
362
363 $this->onCompletionCallbacks = [];
364 }
365
366 /**
367 * @param Throwable $reason
368 * @return void
369 */
370 protected function partFailed(Throwable $reason): void
371 {
372 $this->operationFailed($reason);
373 }
374
375 /**
376 * @return int
377 */
378 protected function calculatePartSize(): int
379 {
380 return max(
381 $this->getTotalSize() / self::PART_MAX_NUM,
382 $this->config['target_part_size_bytes']
383 );
384 }
385 }