| 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 |
|
| 20 |
|
| 21 |
abstract class AbstractMultipartUploader implements PromisorInterface |
| 22 |
{ |
| 23 |
public const PART_MIN_SIZE = 5 * 1024 * 1024; |
| 24 |
public const PART_MAX_SIZE = 5 * 1024 * 1024 * 1024; |
| 25 |
public const PART_MAX_NUM = 10000; |
| 26 |
|
| 27 |
|
| 28 |
protected readonly S3ClientInterface $s3Client; |
| 29 |
|
| 30 |
|
| 31 |
protected readonly array $requestArgs; |
| 32 |
|
| 33 |
|
| 34 |
protected readonly array $config; |
| 35 |
|
| 36 |
|
| 37 |
protected string|null $uploadId; |
| 38 |
|
| 39 |
|
| 40 |
protected array $partsCompleted; |
| 41 |
|
| 42 |
|
| 43 |
protected array $onCompletionCallbacks = []; |
| 44 |
|
| 45 |
|
| 46 |
protected ?TransferListenerNotifier $listenerNotifier; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
protected ?TransferProgressSnapshot $currentSnapshot; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
|
| 84 |
|
| 85 |
abstract protected function createMultipartOperation(): PromiseInterface; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
abstract protected function completeMultipartOperation(): PromiseInterface; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
abstract protected function processMultipartOperation(): PromiseInterface; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
abstract protected function partCompleted( |
| 105 |
int $partSize, |
| 106 |
array $requestArgs, |
| 107 |
array $partData |
| 108 |
): void; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
abstract protected function abortMultipartOperation(): PromiseInterface; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
abstract protected function getTotalSize(): int; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
abstract protected function createResponse(ResultInterface $result): mixed; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 154 |
|
| 155 |
public function getUploadId(): ?string |
| 156 |
{ |
| 157 |
return $this->uploadId; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function getPartsCompleted(): array |
| 164 |
{ |
| 165 |
return $this->partsCompleted; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public function getCurrentSnapshot(): ?TransferProgressSnapshot |
| 173 |
{ |
| 174 |
return $this->currentSnapshot; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 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 |
|
| 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 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 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 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 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 |
|
| 268 |
|
| 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 |
|
| 288 |
|
| 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 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
protected function operationFailed(Throwable $reason): void |
| 315 |
{ |
| 316 |
|
| 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 |
|
| 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 |
|
| 368 |
|
| 369 |
|
| 370 |
protected function partFailed(Throwable $reason): void |
| 371 |
{ |
| 372 |
$this->operationFailed($reason); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 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 |
} |