| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\S3\S3Transfer; |
| 4 |
|
| 5 |
use Aws\MetricsBuilder; |
| 6 |
use Aws\S3\S3Transfer\Exception\S3TransferException; |
| 7 |
use Aws\S3\S3Transfer\Models\UploadDirectoryRequest; |
| 8 |
use Aws\S3\S3Transfer\Models\UploadDirectoryResult; |
| 9 |
use Aws\S3\S3Transfer\Models\UploadRequest; |
| 10 |
use Aws\S3\S3Transfer\Models\UploadResult; |
| 11 |
use Aws\S3\S3Transfer\Progress\DirectoryProgressTracker; |
| 12 |
use Aws\S3\S3Transfer\Progress\DirectoryTransferProgressAggregator; |
| 13 |
use Aws\S3\S3ClientInterface; |
| 14 |
use Closure; |
| 15 |
use FilesystemIterator; |
| 16 |
use GuzzleHttp\Promise\Each; |
| 17 |
use GuzzleHttp\Promise\PromiseInterface; |
| 18 |
use GuzzleHttp\Promise\PromisorInterface; |
| 19 |
use RecursiveDirectoryIterator; |
| 20 |
use RecursiveIteratorIterator; |
| 21 |
use Throwable; |
| 22 |
use function Aws\filter; |
| 23 |
|
| 24 |
final class DirectoryUploader implements PromisorInterface |
| 25 |
{ |
| 26 |
|
| 27 |
private array $config; |
| 28 |
|
| 29 |
|
| 30 |
private S3ClientInterface $s3Client; |
| 31 |
|
| 32 |
|
| 33 |
private Closure $uploadObject; |
| 34 |
|
| 35 |
|
| 36 |
private int $objectsUploaded = 0; |
| 37 |
|
| 38 |
|
| 39 |
private int $objectsFailed = 0; |
| 40 |
|
| 41 |
|
| 42 |
private UploadDirectoryRequest $uploadDirectoryRequest; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct( |
| 52 |
S3ClientInterface $s3Client, |
| 53 |
array $config, |
| 54 |
Closure $uploadObject, |
| 55 |
UploadDirectoryRequest $uploadDirectoryRequest |
| 56 |
) { |
| 57 |
$this->s3Client = $s3Client; |
| 58 |
$this->config = $config; |
| 59 |
$this->uploadObject = $uploadObject; |
| 60 |
$this->uploadDirectoryRequest = $uploadDirectoryRequest; |
| 61 |
|
| 62 |
|
| 63 |
$this->uploadDirectoryRequest->updateConfigWithDefaults( |
| 64 |
$this->config |
| 65 |
); |
| 66 |
$this->uploadDirectoryRequest->validateSourceDirectory(); |
| 67 |
$this->uploadDirectoryRequest->validateConfig(); |
| 68 |
|
| 69 |
MetricsBuilder::appendMetricsCaptureMiddleware( |
| 70 |
$this->s3Client->getHandlerList(), |
| 71 |
MetricsBuilder::S3_TRANSFER_UPLOAD_DIRECTORY |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function promise(): PromiseInterface |
| 81 |
{ |
| 82 |
$this->objectsUploaded = 0; |
| 83 |
$this->objectsFailed = 0; |
| 84 |
|
| 85 |
$config = $this->uploadDirectoryRequest->getConfig(); |
| 86 |
|
| 87 |
$filter = $config['filter'] ?? null; |
| 88 |
$uploadObjectRequestModifier = $config['upload_object_request_modifier'] |
| 89 |
?? null; |
| 90 |
$failurePolicyCallback = $config['failure_policy'] ?? null; |
| 91 |
|
| 92 |
$sourceDirectory = $this->uploadDirectoryRequest->getSourceDirectory(); |
| 93 |
$files = $this->iterateSourceFiles( |
| 94 |
$sourceDirectory, |
| 95 |
$config, |
| 96 |
$filter |
| 97 |
); |
| 98 |
|
| 99 |
$baseDir = rtrim($sourceDirectory, '/') . DIRECTORY_SEPARATOR; |
| 100 |
$delimiter = $config['s3_delimiter'] ?? '/'; |
| 101 |
$s3Prefix = $config['s3_prefix'] ?? ''; |
| 102 |
if ($s3Prefix !== '' && !str_ends_with($s3Prefix, '/')) { |
| 103 |
$s3Prefix .= '/'; |
| 104 |
} |
| 105 |
|
| 106 |
$targetBucket = $this->uploadDirectoryRequest->getTargetBucket(); |
| 107 |
|
| 108 |
$directoryProgressTracker = $this->uploadDirectoryRequest->getProgressTracker(); |
| 109 |
if ($directoryProgressTracker === null |
| 110 |
&& ($config['track_progress'] |
| 111 |
?? ($this->config['track_progress'] ?? false))) { |
| 112 |
$directoryProgressTracker = new DirectoryProgressTracker(); |
| 113 |
} |
| 114 |
|
| 115 |
$directoryListeners = $this->uploadDirectoryRequest->getListeners(); |
| 116 |
$singleObjectListeners = $this->uploadDirectoryRequest->getSingleObjectListeners(); |
| 117 |
$aggregator = new DirectoryTransferProgressAggregator( |
| 118 |
identifier: $this->buildDirectoryIdentifier( |
| 119 |
$sourceDirectory, |
| 120 |
$targetBucket, |
| 121 |
$s3Prefix |
| 122 |
), |
| 123 |
totalBytes: 0, |
| 124 |
totalFiles: 0, |
| 125 |
directoryListeners: $directoryListeners, |
| 126 |
directoryProgressTracker: $directoryProgressTracker, |
| 127 |
); |
| 128 |
|
| 129 |
$maxConcurrency = $config['max_concurrency'] |
| 130 |
?? UploadDirectoryRequest::DEFAULT_MAX_CONCURRENCY; |
| 131 |
|
| 132 |
$aggregator->notifyDirectoryInitiated([ |
| 133 |
'source_directory' => $sourceDirectory, |
| 134 |
'bucket' => $targetBucket, |
| 135 |
's3_prefix' => $s3Prefix, |
| 136 |
]); |
| 137 |
|
| 138 |
return Each::ofLimitAll( |
| 139 |
$this->createUploadPromises( |
| 140 |
$files, |
| 141 |
$config, |
| 142 |
$uploadObjectRequestModifier, |
| 143 |
$failurePolicyCallback, |
| 144 |
$sourceDirectory, |
| 145 |
$targetBucket, |
| 146 |
$baseDir, |
| 147 |
$delimiter, |
| 148 |
$s3Prefix, |
| 149 |
$aggregator, |
| 150 |
$singleObjectListeners |
| 151 |
), |
| 152 |
$maxConcurrency |
| 153 |
)->then(function () use ($aggregator) { |
| 154 |
$aggregator->notifyDirectoryComplete([ |
| 155 |
'objects_uploaded' => $this->objectsUploaded, |
| 156 |
'objects_failed' => $this->objectsFailed, |
| 157 |
]); |
| 158 |
return new UploadDirectoryResult( |
| 159 |
$this->objectsUploaded, |
| 160 |
$this->objectsFailed |
| 161 |
); |
| 162 |
})->otherwise(function (Throwable $reason) use ($aggregator) { |
| 163 |
$aggregator->notifyDirectoryFail($reason); |
| 164 |
return new UploadDirectoryResult( |
| 165 |
$this->objectsUploaded, |
| 166 |
$this->objectsFailed, |
| 167 |
$reason |
| 168 |
); |
| 169 |
}); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private function createUploadPromises( |
| 189 |
iterable $files, |
| 190 |
array $config, |
| 191 |
?callable $uploadObjectRequestModifier, |
| 192 |
?callable $failurePolicyCallback, |
| 193 |
string $sourceDirectory, |
| 194 |
string $targetBucket, |
| 195 |
string $baseDir, |
| 196 |
string $delimiter, |
| 197 |
string $s3Prefix, |
| 198 |
DirectoryTransferProgressAggregator $aggregator, |
| 199 |
array $singleObjectListeners |
| 200 |
): \Generator |
| 201 |
{ |
| 202 |
foreach ($files as $file) { |
| 203 |
$fileSize = filesize($file); |
| 204 |
$aggregator->incrementTotals( |
| 205 |
$fileSize !== false ? $fileSize : 0 |
| 206 |
); |
| 207 |
|
| 208 |
$relativePath = substr($file, strlen($baseDir)); |
| 209 |
if (str_contains($relativePath, $delimiter) && $delimiter !== '/') { |
| 210 |
throw new S3TransferException( |
| 211 |
"The filename `$relativePath` must not contain the provided delimiter `$delimiter`" |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
$objectKey = $s3Prefix.$relativePath; |
| 216 |
$objectKey = str_replace( |
| 217 |
DIRECTORY_SEPARATOR, |
| 218 |
$delimiter, |
| 219 |
$objectKey |
| 220 |
); |
| 221 |
$uploadRequestArgs = $this->uploadDirectoryRequest->getUploadRequestArgs(); |
| 222 |
$uploadRequestArgs['Bucket'] = $targetBucket; |
| 223 |
$uploadRequestArgs['Key'] = $objectKey; |
| 224 |
|
| 225 |
if ($uploadObjectRequestModifier !== null) { |
| 226 |
$uploadObjectRequestModifier($uploadRequestArgs); |
| 227 |
} |
| 228 |
|
| 229 |
$uploadObject = $this->uploadObject; |
| 230 |
$uploadConfig = $config; |
| 231 |
$uploadConfig['track_progress'] = false; |
| 232 |
yield $uploadObject( |
| 233 |
$this->s3Client, |
| 234 |
new UploadRequest( |
| 235 |
$file, |
| 236 |
$uploadRequestArgs, |
| 237 |
$uploadConfig, |
| 238 |
listeners: array_merge( |
| 239 |
[$aggregator], |
| 240 |
array_map( |
| 241 |
fn($listener) => clone $listener, |
| 242 |
$singleObjectListeners |
| 243 |
) |
| 244 |
), |
| 245 |
progressTracker: null |
| 246 |
) |
| 247 |
)->then(function (UploadResult $response) { |
| 248 |
$this->objectsUploaded++; |
| 249 |
|
| 250 |
return $response; |
| 251 |
})->otherwise(function (Throwable $reason) use ( |
| 252 |
$targetBucket, |
| 253 |
$sourceDirectory, |
| 254 |
$failurePolicyCallback, |
| 255 |
$uploadRequestArgs |
| 256 |
) { |
| 257 |
$this->objectsFailed++; |
| 258 |
if($failurePolicyCallback !== null) { |
| 259 |
call_user_func( |
| 260 |
$failurePolicyCallback, |
| 261 |
$uploadRequestArgs, |
| 262 |
[ |
| 263 |
"source_directory" => $sourceDirectory, |
| 264 |
"bucket_to" => $targetBucket, |
| 265 |
], |
| 266 |
$reason, |
| 267 |
new UploadDirectoryResult( |
| 268 |
$this->objectsUploaded, |
| 269 |
$this->objectsFailed |
| 270 |
) |
| 271 |
); |
| 272 |
|
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
throw $reason; |
| 277 |
}); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
private function iterateSourceFiles( |
| 291 |
string $sourceDirectory, |
| 292 |
array $config, |
| 293 |
?callable $filter |
| 294 |
): \Generator { |
| 295 |
$dirIterator = new RecursiveDirectoryIterator($sourceDirectory); |
| 296 |
|
| 297 |
$flags = FilesystemIterator::SKIP_DOTS; |
| 298 |
if ($config['follow_symbolic_links'] ?? false) { |
| 299 |
$flags |= FilesystemIterator::FOLLOW_SYMLINKS; |
| 300 |
} |
| 301 |
|
| 302 |
$dirIterator->setFlags($flags); |
| 303 |
|
| 304 |
if ($config['recursive'] ?? false) { |
| 305 |
$dirIterator = new RecursiveIteratorIterator( |
| 306 |
$dirIterator, |
| 307 |
RecursiveIteratorIterator::SELF_FIRST |
| 308 |
); |
| 309 |
if (isset($config['max_depth'])) { |
| 310 |
$dirIterator->setMaxDepth($config['max_depth']); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
$dirVisited = []; |
| 315 |
$files = filter( |
| 316 |
$dirIterator, |
| 317 |
function ($file) use ($filter, &$dirVisited) { |
| 318 |
if (is_dir($file)) { |
| 319 |
|
| 320 |
$dirRealPath = realpath($file); |
| 321 |
if ($dirRealPath !== false) { |
| 322 |
if ($dirVisited[$dirRealPath] ?? false) { |
| 323 |
throw new S3TransferException( |
| 324 |
"A circular symbolic link traversal has been detected at $file -> $dirRealPath" |
| 325 |
); |
| 326 |
} |
| 327 |
|
| 328 |
$dirVisited[$dirRealPath] = true; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
if ($filter !== null) { |
| 333 |
return !is_dir($file) && $filter($file); |
| 334 |
} |
| 335 |
|
| 336 |
return !is_dir($file); |
| 337 |
} |
| 338 |
); |
| 339 |
|
| 340 |
foreach ($files as $file) { |
| 341 |
yield $file; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
private function buildDirectoryIdentifier( |
| 353 |
string $sourceDirectory, |
| 354 |
string $bucket, |
| 355 |
string $s3Prefix |
| 356 |
): string { |
| 357 |
return sprintf( |
| 358 |
'upload:%s->%s/%s', |
| 359 |
rtrim($sourceDirectory, DIRECTORY_SEPARATOR), |
| 360 |
$bucket, |
| 361 |
$s3Prefix |
| 362 |
); |
| 363 |
} |
| 364 |
} |