| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\S3\S3Transfer; |
| 4 |
|
| 5 |
use Aws\MetricsBuilder; |
| 6 |
use Aws\S3\S3ClientInterface; |
| 7 |
use Aws\S3\S3Transfer\Exception\S3TransferException; |
| 8 |
use Aws\S3\S3Transfer\Models\DownloadDirectoryRequest; |
| 9 |
use Aws\S3\S3Transfer\Models\DownloadDirectoryResult; |
| 10 |
use Aws\S3\S3Transfer\Models\DownloadFileRequest; |
| 11 |
use Aws\S3\S3Transfer\Models\DownloadRequest; |
| 12 |
use Aws\S3\S3Transfer\Progress\DirectoryProgressTracker; |
| 13 |
use Aws\S3\S3Transfer\Progress\DirectoryTransferProgressAggregator; |
| 14 |
use Closure; |
| 15 |
use GuzzleHttp\Promise\Each; |
| 16 |
use GuzzleHttp\Promise\PromiseInterface; |
| 17 |
use GuzzleHttp\Promise\PromisorInterface; |
| 18 |
use Throwable; |
| 19 |
use function Aws\filter; |
| 20 |
use function Aws\map; |
| 21 |
|
| 22 |
final class DirectoryDownloader implements PromisorInterface |
| 23 |
{ |
| 24 |
|
| 25 |
private S3ClientInterface $s3Client; |
| 26 |
|
| 27 |
|
| 28 |
private array $config; |
| 29 |
|
| 30 |
|
| 31 |
private Closure $downloadFile; |
| 32 |
|
| 33 |
|
| 34 |
private int $objectsDownloaded = 0; |
| 35 |
|
| 36 |
|
| 37 |
private int $objectsFailed = 0; |
| 38 |
|
| 39 |
|
| 40 |
private DownloadDirectoryRequest $downloadDirectoryRequest; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function __construct( |
| 49 |
S3ClientInterface $s3Client, |
| 50 |
array $config, |
| 51 |
Closure $downloadFile, |
| 52 |
DownloadDirectoryRequest $downloadDirectoryRequest |
| 53 |
) { |
| 54 |
$this->s3Client = $s3Client; |
| 55 |
$this->config = $config; |
| 56 |
$this->downloadFile = $downloadFile; |
| 57 |
$this->downloadDirectoryRequest = $downloadDirectoryRequest; |
| 58 |
|
| 59 |
|
| 60 |
$this->downloadDirectoryRequest->updateConfigWithDefaults( |
| 61 |
$this->config |
| 62 |
); |
| 63 |
$this->downloadDirectoryRequest->validateConfig(); |
| 64 |
$this->downloadDirectoryRequest->validateDestinationDirectory(); |
| 65 |
|
| 66 |
MetricsBuilder::appendMetricsCaptureMiddleware( |
| 67 |
$this->s3Client->getHandlerList(), |
| 68 |
MetricsBuilder::S3_TRANSFER_DOWNLOAD_DIRECTORY |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function promise(): PromiseInterface |
| 78 |
{ |
| 79 |
$this->objectsDownloaded = 0; |
| 80 |
$this->objectsFailed = 0; |
| 81 |
|
| 82 |
$destinationDirectory = $this->downloadDirectoryRequest->getDestinationDirectory(); |
| 83 |
$sourceBucket = $this->downloadDirectoryRequest->getSourceBucket(); |
| 84 |
$progressTracker = $this->downloadDirectoryRequest->getProgressTracker(); |
| 85 |
|
| 86 |
$config = $this->downloadDirectoryRequest->getConfig(); |
| 87 |
if ($progressTracker === null && $config['track_progress']) { |
| 88 |
$progressTracker = new DirectoryProgressTracker(); |
| 89 |
} |
| 90 |
|
| 91 |
$listArgs = [ |
| 92 |
'Bucket' => $sourceBucket, |
| 93 |
] + ($config['list_objects_v2_args'] ?? []); |
| 94 |
|
| 95 |
$s3Prefix = $config['s3_prefix'] ?? null; |
| 96 |
if (empty($listArgs['Prefix']) && $s3Prefix !== null) { |
| 97 |
$listArgs['Prefix'] = $s3Prefix; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
$listArgs['Delimiter'] = null; |
| 102 |
|
| 103 |
$objects = $this->s3Client |
| 104 |
->getPaginator('ListObjectsV2', $listArgs) |
| 105 |
->search('Contents[]'); |
| 106 |
|
| 107 |
$filter = $config['filter'] ?? null; |
| 108 |
$objects = filter($objects, function (array $object) use ($filter) { |
| 109 |
$key = $object['Key'] ?? ''; |
| 110 |
if ($filter !== null) { |
| 111 |
return call_user_func($filter, $key) && !str_ends_with($key, "/"); |
| 112 |
} |
| 113 |
|
| 114 |
return !str_ends_with($key, "/"); |
| 115 |
}); |
| 116 |
$objects = map($objects, function (array $object) use ($sourceBucket) { |
| 117 |
return [ |
| 118 |
'uri' => self::formatAsS3URI($sourceBucket, $object['Key']), |
| 119 |
'size' => $object['Size'] ?? 0, |
| 120 |
]; |
| 121 |
}); |
| 122 |
|
| 123 |
$downloadObjectRequestModifier = $config['download_object_request_modifier'] |
| 124 |
?? null; |
| 125 |
$failurePolicyCallback = $config['failure_policy'] ?? null; |
| 126 |
|
| 127 |
$directoryListeners = $this->downloadDirectoryRequest->getListeners(); |
| 128 |
$singleObjectListeners = $this->downloadDirectoryRequest->getSingleObjectListeners(); |
| 129 |
$aggregator = new DirectoryTransferProgressAggregator( |
| 130 |
identifier: $this->buildDirectoryIdentifier( |
| 131 |
$sourceBucket, |
| 132 |
$destinationDirectory, |
| 133 |
$s3Prefix |
| 134 |
), |
| 135 |
totalBytes: 0, |
| 136 |
totalFiles: 0, |
| 137 |
directoryListeners: $directoryListeners, |
| 138 |
directoryProgressTracker: $progressTracker |
| 139 |
); |
| 140 |
|
| 141 |
$maxConcurrency = $config['max_concurrency'] |
| 142 |
?? DownloadDirectoryRequest::DEFAULT_MAX_CONCURRENCY; |
| 143 |
|
| 144 |
$aggregator->notifyDirectoryInitiated([ |
| 145 |
'bucket' => $sourceBucket, |
| 146 |
'destination_directory' => $destinationDirectory, |
| 147 |
's3_prefix' => $s3Prefix, |
| 148 |
]); |
| 149 |
|
| 150 |
return Each::ofLimitAll( |
| 151 |
$this->createDownloadPromises( |
| 152 |
$objects, |
| 153 |
$config, |
| 154 |
$destinationDirectory, |
| 155 |
$sourceBucket, |
| 156 |
$s3Prefix, |
| 157 |
$downloadObjectRequestModifier, |
| 158 |
$failurePolicyCallback, |
| 159 |
$aggregator, |
| 160 |
$singleObjectListeners |
| 161 |
), |
| 162 |
$maxConcurrency |
| 163 |
)->then(function () use ($aggregator) { |
| 164 |
$aggregator->notifyDirectoryComplete([ |
| 165 |
'objects_downloaded' => $this->objectsDownloaded, |
| 166 |
'objects_failed' => $this->objectsFailed, |
| 167 |
]); |
| 168 |
return new DownloadDirectoryResult( |
| 169 |
$this->objectsDownloaded, |
| 170 |
$this->objectsFailed |
| 171 |
); |
| 172 |
})->otherwise(function (Throwable $reason) use ($aggregator) { |
| 173 |
$aggregator->notifyDirectoryFail($reason); |
| 174 |
return new DownloadDirectoryResult( |
| 175 |
$this->objectsDownloaded, |
| 176 |
$this->objectsFailed, |
| 177 |
$reason |
| 178 |
); |
| 179 |
}); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
private function createDownloadPromises( |
| 197 |
iterable $objects, |
| 198 |
array $config, |
| 199 |
string $destinationDirectory, |
| 200 |
string $sourceBucket, |
| 201 |
?string $s3Prefix, |
| 202 |
?callable $downloadObjectRequestModifier, |
| 203 |
?callable $failurePolicyCallback, |
| 204 |
DirectoryTransferProgressAggregator $aggregator, |
| 205 |
array $singleObjectListeners |
| 206 |
): \Generator |
| 207 |
{ |
| 208 |
$s3Delimiter = '/'; |
| 209 |
foreach ($objects as $object) { |
| 210 |
$aggregator->incrementTotals($object['size'] ?? 0); |
| 211 |
$bucketAndKeyArray = S3TransferManager::s3UriAsBucketAndKey($object['uri']); |
| 212 |
$objectKey = $bucketAndKeyArray['Key']; |
| 213 |
if ($s3Prefix !== null && str_contains($objectKey, $s3Delimiter)) { |
| 214 |
$prefixToStrip = str_ends_with($s3Prefix, $s3Delimiter) |
| 215 |
? $s3Prefix |
| 216 |
: $s3Prefix . $s3Delimiter; |
| 217 |
$objectKey = substr($objectKey, strlen($prefixToStrip)); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
if (DIRECTORY_SEPARATOR !== $s3Delimiter) { |
| 222 |
$objectKey = str_replace( |
| 223 |
$s3Delimiter, |
| 224 |
DIRECTORY_SEPARATOR, |
| 225 |
$objectKey |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
$destinationFile = $destinationDirectory . DIRECTORY_SEPARATOR . $objectKey; |
| 230 |
if ($this->resolvesOutsideTargetDirectory($destinationFile, $objectKey)) { |
| 231 |
throw new S3TransferException( |
| 232 |
"Cannot download key $objectKey " |
| 233 |
."its relative path resolves outside the parent directory." |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
$requestArgs = $this->downloadDirectoryRequest->getDownloadRequestArgs(); |
| 238 |
foreach ($bucketAndKeyArray as $key => $value) { |
| 239 |
$requestArgs[$key] = $value; |
| 240 |
} |
| 241 |
if ($downloadObjectRequestModifier !== null) { |
| 242 |
call_user_func($downloadObjectRequestModifier, $requestArgs); |
| 243 |
} |
| 244 |
|
| 245 |
$downloadFile = $this->downloadFile; |
| 246 |
$downloadConfig = $config; |
| 247 |
$downloadConfig['track_progress'] = false; |
| 248 |
yield $downloadFile( |
| 249 |
$this->s3Client, |
| 250 |
new DownloadFileRequest( |
| 251 |
destination: $destinationFile, |
| 252 |
failsWhenDestinationExists: $config['fails_when_destination_exists'] ?? false, |
| 253 |
downloadRequest: new DownloadRequest( |
| 254 |
source: null, |
| 255 |
downloadRequestArgs: $requestArgs, |
| 256 |
config: array_merge( |
| 257 |
$downloadConfig, |
| 258 |
[ |
| 259 |
'target_part_size_bytes' => $config['target_part_size_bytes'] ?? 0, |
| 260 |
] |
| 261 |
), |
| 262 |
downloadHandler: null, |
| 263 |
listeners: array_merge( |
| 264 |
[$aggregator], |
| 265 |
array_map( |
| 266 |
fn($listener) => clone $listener, |
| 267 |
$singleObjectListeners |
| 268 |
) |
| 269 |
), |
| 270 |
progressTracker: null |
| 271 |
) |
| 272 |
), |
| 273 |
)->then(function () { |
| 274 |
$this->objectsDownloaded++; |
| 275 |
})->otherwise(function (Throwable $reason) use ( |
| 276 |
$sourceBucket, |
| 277 |
$destinationDirectory, |
| 278 |
$failurePolicyCallback, |
| 279 |
$requestArgs |
| 280 |
) { |
| 281 |
$this->objectsFailed++; |
| 282 |
if ($failurePolicyCallback !== null) { |
| 283 |
call_user_func( |
| 284 |
$failurePolicyCallback, |
| 285 |
$requestArgs, |
| 286 |
[ |
| 287 |
"destination_directory" => $destinationDirectory, |
| 288 |
"bucket" => $sourceBucket, |
| 289 |
], |
| 290 |
$reason, |
| 291 |
new DownloadDirectoryResult( |
| 292 |
$this->objectsDownloaded, |
| 293 |
$this->objectsFailed |
| 294 |
) |
| 295 |
); |
| 296 |
|
| 297 |
return; |
| 298 |
} |
| 299 |
|
| 300 |
throw $reason; |
| 301 |
}); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
private static function formatAsS3URI(string $bucket, string $key): string |
| 312 |
{ |
| 313 |
return "s3://$bucket/$key"; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
private function resolvesOutsideTargetDirectory( |
| 323 |
string $sink, |
| 324 |
string $objectKey |
| 325 |
): bool |
| 326 |
{ |
| 327 |
$resolved = []; |
| 328 |
$sections = explode(DIRECTORY_SEPARATOR, $sink); |
| 329 |
$targetSectionsLength = count(explode(DIRECTORY_SEPARATOR, $objectKey)); |
| 330 |
$targetSections = array_slice($sections, -($targetSectionsLength + 1)); |
| 331 |
$targetDirectory = $targetSections[0]; |
| 332 |
|
| 333 |
foreach ($targetSections as $section) { |
| 334 |
if ($section === '.' || $section === '') { |
| 335 |
continue; |
| 336 |
} |
| 337 |
if ($section === '..') { |
| 338 |
array_pop($resolved); |
| 339 |
if (empty($resolved) || $resolved[0] !== $targetDirectory) { |
| 340 |
return true; |
| 341 |
} |
| 342 |
} else { |
| 343 |
$resolved []= $section; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
private function buildDirectoryIdentifier( |
| 358 |
string $bucket, |
| 359 |
string $destinationDirectory, |
| 360 |
?string $s3Prefix |
| 361 |
): string { |
| 362 |
return sprintf( |
| 363 |
'download:%s/%s->%s', |
| 364 |
$bucket, |
| 365 |
$s3Prefix ?? '', |
| 366 |
rtrim($destinationDirectory, DIRECTORY_SEPARATOR) |
| 367 |
); |
| 368 |
} |
| 369 |
} |