diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index ae47d7e5fd..78f5b6a2f5 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -55,6 +55,10 @@ class MultipartUploader extends AbstractUploader * of the multipart upload and that is used to resume a previous upload. * When this option is provided, the `bucket`, `key`, and `part_size` * options are ignored. + * - add_content_md5: (boolean) Set to true to automatically calculate the + * MD5 checksum for the upload. + * - checksum_algorithm: (string, default=string(CRC32)) S3 Checksum + * Algorithm to use for upload. * * @param S3ClientInterface $client Client used for the upload. * @param mixed $source Source of the data to upload. @@ -70,6 +74,14 @@ public function __construct( 'key' => null, 'exception_class' => S3MultipartUploadException::class, ]); + + if (isset($config['checksum_algorithm'])) { + $this->checksumAlgorithm = strtoupper($config['checksum_algorithm']); + $this->checksumParam = 'Checksum'.$this->checksumAlgorithm; + } else { + $this->checksumAlgorithm = 'CRC32'; + $this->checksumParam = 'ChecksumCRC32'; + } } protected function loadUploadWorkflowInfo() @@ -133,6 +145,12 @@ protected function createPart($seekable, $number) $data['AddContentMD5'] = true; } + $data['ChecksumAlgorithm'] = $this->checksumAlgorithm; + $data[$this->checksumParam] = CalculatesChecksumTrait::getEncodedValue( + $this->checksumAlgorithm, + $body + ); + $data['ContentLength'] = $contentLength; return $data; diff --git a/src/S3/MultipartUploadingTrait.php b/src/S3/MultipartUploadingTrait.php index baccf58c51..912be874bf 100644 --- a/src/S3/MultipartUploadingTrait.php +++ b/src/S3/MultipartUploadingTrait.php @@ -54,6 +54,7 @@ protected function handleResult(CommandInterface $command, ResultInterface $resu $this->getState()->markPartAsUploaded($command['PartNumber'], [ 'PartNumber' => $command['PartNumber'], 'ETag' => $this->extractETag($result), + $this->checksumParam => $result[$this->checksumParam] ]); } @@ -67,6 +68,10 @@ protected function getCompleteParams() $params['MultipartUpload'] = [ 'Parts' => $this->getState()->getUploadedParts() ]; + $params['MultipartUpload'][$this->checksumParam] = + CalculatesChecksumTrait::getEncodedValue( + $this->checksumAlgorithm, $this->source + ); return $params; } @@ -107,6 +112,8 @@ protected function getInitiateParams() $params['ContentType'] = $type; } + $params['ChecksumAlgorithm'] = $this->checksumAlgorithm; + return $params; }