| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\S3; |
| 4 |
|
| 5 |
use Aws\Arn\ArnParser; |
| 6 |
use Aws\Multipart\AbstractUploadManager; |
| 7 |
use Aws\ResultInterface; |
| 8 |
use GuzzleHttp\Psr7; |
| 9 |
|
| 10 |
class MultipartCopy extends AbstractUploadManager |
| 11 |
{ |
| 12 |
use MultipartUploadingTrait { |
| 13 |
getInitiateParams as private traitGetInitiateParams; |
| 14 |
} |
| 15 |
|
| 16 |
private const VALID_METADATA_DIRECTIVES = [ |
| 17 |
'COPY' => true, |
| 18 |
'REPLACE' => true, |
| 19 |
]; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private static array $copyMetadataFields = [ |
| 26 |
'CacheControl', |
| 27 |
'ContentDisposition', |
| 28 |
'ContentEncoding', |
| 29 |
'ContentLanguage', |
| 30 |
'ContentType', |
| 31 |
'Expires', |
| 32 |
'Metadata', |
| 33 |
]; |
| 34 |
|
| 35 |
|
| 36 |
private $source; |
| 37 |
|
| 38 |
private $sourceVersionId; |
| 39 |
|
| 40 |
private $sourceMetadata; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function __construct( |
| 97 |
S3ClientInterface $client, |
| 98 |
$source, |
| 99 |
array $config = [] |
| 100 |
) { |
| 101 |
if (is_array($source)) { |
| 102 |
$this->source = $source; |
| 103 |
} else { |
| 104 |
$this->source = $this->getInputSource($source); |
| 105 |
} |
| 106 |
|
| 107 |
parent::__construct( |
| 108 |
$client, |
| 109 |
array_change_key_case($config) + ['source_metadata' => null] |
| 110 |
); |
| 111 |
|
| 112 |
if ($this->displayProgress) { |
| 113 |
$this->getState()->setProgressThresholds( |
| 114 |
$this->sourceMetadata["ContentLength"] |
| 115 |
); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function copy() |
| 125 |
{ |
| 126 |
return $this->upload(); |
| 127 |
} |
| 128 |
|
| 129 |
protected function loadUploadWorkflowInfo() |
| 130 |
{ |
| 131 |
return [ |
| 132 |
'command' => [ |
| 133 |
'initiate' => 'CreateMultipartUpload', |
| 134 |
'upload' => 'UploadPartCopy', |
| 135 |
'complete' => 'CompleteMultipartUpload', |
| 136 |
], |
| 137 |
'id' => [ |
| 138 |
'bucket' => 'Bucket', |
| 139 |
'key' => 'Key', |
| 140 |
'upload_id' => 'UploadId', |
| 141 |
], |
| 142 |
'part_num' => 'PartNumber', |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
protected function getUploadCommands(callable $resultHandler) |
| 147 |
{ |
| 148 |
$parts = ceil($this->getSourceSize() / $this->determinePartSize()); |
| 149 |
|
| 150 |
for ($partNumber = 1; $partNumber <= $parts; $partNumber++) { |
| 151 |
|
| 152 |
if (!$this->state->hasPartBeenUploaded($partNumber)) { |
| 153 |
$command = $this->client->getCommand( |
| 154 |
$this->info['command']['upload'], |
| 155 |
$this->createPart($partNumber, $parts) + $this->getState()->getId() |
| 156 |
); |
| 157 |
$command->getHandlerList()->appendSign($resultHandler, 'mup'); |
| 158 |
yield $command; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
private function createPart($partNumber, $partsCount) |
| 164 |
{ |
| 165 |
$data = []; |
| 166 |
|
| 167 |
|
| 168 |
$config = $this->getConfig(); |
| 169 |
$params = isset($config['params']) ? $config['params'] : []; |
| 170 |
foreach ($params as $k => $v) { |
| 171 |
$data[$k] = $v; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
if (is_array($this->source)) { |
| 176 |
$key = str_replace('%2F', '/', rawurlencode($this->source['source_key'])); |
| 177 |
$bucket = $this->source['source_bucket']; |
| 178 |
} else { |
| 179 |
list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2); |
| 180 |
$key = implode( |
| 181 |
'/', |
| 182 |
array_map( |
| 183 |
'urlencode', |
| 184 |
explode('/', rawurldecode($key)) |
| 185 |
) |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
$uri = ArnParser::isArn($bucket) ? '' : '/'; |
| 190 |
$uri .= $bucket . '/' . $key; |
| 191 |
$data['CopySource'] = $uri; |
| 192 |
$data['PartNumber'] = $partNumber; |
| 193 |
if (!empty($this->sourceVersionId)) { |
| 194 |
$data['CopySource'] .= "?versionId=" . $this->sourceVersionId; |
| 195 |
} |
| 196 |
|
| 197 |
$defaultPartSize = $this->determinePartSize(); |
| 198 |
$startByte = $defaultPartSize * ($partNumber - 1); |
| 199 |
$data['ContentLength'] = $partNumber < $partsCount |
| 200 |
? $defaultPartSize |
| 201 |
: $this->getSourceSize() - ($defaultPartSize * ($partsCount - 1)); |
| 202 |
$endByte = $startByte + $data['ContentLength'] - 1; |
| 203 |
$data['CopySourceRange'] = "bytes=$startByte-$endByte"; |
| 204 |
|
| 205 |
return $data; |
| 206 |
} |
| 207 |
|
| 208 |
protected function extractETag(ResultInterface $result) |
| 209 |
{ |
| 210 |
return $result->search('CopyPartResult.ETag'); |
| 211 |
} |
| 212 |
|
| 213 |
protected function getInitiateParams() |
| 214 |
{ |
| 215 |
$params = $this->traitGetInitiateParams(); |
| 216 |
|
| 217 |
$directive = strtoupper($this->config['metadata_directive'] ?? 'COPY'); |
| 218 |
|
| 219 |
if (!isset(self::VALID_METADATA_DIRECTIVES[$directive])) { |
| 220 |
throw new \InvalidArgumentException( |
| 221 |
"Invalid metadata_directive value '$directive'." |
| 222 |
. " Must be 'COPY' or 'REPLACE'." |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
if ($directive === 'COPY') { |
| 227 |
$sourceMetadata = $this->getSourceMetadata(); |
| 228 |
foreach (self::$copyMetadataFields as $field) { |
| 229 |
if (!empty($sourceMetadata[$field])) { |
| 230 |
$params[$field] = $sourceMetadata[$field]; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
return $params; |
| 236 |
} |
| 237 |
|
| 238 |
protected function getSourceMimeType() |
| 239 |
{ |
| 240 |
return $this->getSourceMetadata()['ContentType']; |
| 241 |
} |
| 242 |
|
| 243 |
protected function getSourceSize() |
| 244 |
{ |
| 245 |
return $this->getSourceMetadata()['ContentLength']; |
| 246 |
} |
| 247 |
|
| 248 |
private function getSourceMetadata() |
| 249 |
{ |
| 250 |
if (empty($this->sourceMetadata)) { |
| 251 |
$this->sourceMetadata = $this->fetchSourceMetadata(); |
| 252 |
} |
| 253 |
|
| 254 |
return $this->sourceMetadata; |
| 255 |
} |
| 256 |
|
| 257 |
private function fetchSourceMetadata() |
| 258 |
{ |
| 259 |
if ($this->config['source_metadata'] instanceof ResultInterface) { |
| 260 |
return $this->config['source_metadata']; |
| 261 |
} |
| 262 |
|
| 263 |
if (is_array($this->source)) { |
| 264 |
$headParams = [ |
| 265 |
'Key' => $this->source['source_key'], |
| 266 |
'Bucket' => $this->source['source_bucket'] |
| 267 |
]; |
| 268 |
if (isset($this->source['source_version_id'])) { |
| 269 |
$this->sourceVersionId = $this->source['source_version_id']; |
| 270 |
$headParams['VersionId'] = $this->sourceVersionId; |
| 271 |
} |
| 272 |
|
| 273 |
} else { |
| 274 |
list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2); |
| 275 |
$headParams = [ |
| 276 |
'Bucket' => $bucket, |
| 277 |
'Key' => $key, |
| 278 |
]; |
| 279 |
if (strpos($key, '?')) { |
| 280 |
list($key, $query) = explode('?', $key, 2); |
| 281 |
$headParams['Key'] = $key; |
| 282 |
$query = Psr7\Query::parse($query, false); |
| 283 |
if (isset($query['versionId'])) { |
| 284 |
$this->sourceVersionId = $query['versionId']; |
| 285 |
$headParams['VersionId'] = $this->sourceVersionId; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
return $this->client->headObject($headParams); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
private function getInputSource($inputSource) |
| 300 |
{ |
| 301 |
$sourceBuilder = ArnParser::isArn($inputSource) ? '' : '/'; |
| 302 |
$sourceBuilder .= ltrim(rawurldecode($inputSource), '/'); |
| 303 |
return $sourceBuilder; |
| 304 |
} |
| 305 |
} |