123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace Aws\S3;
- use Aws\HashingStream;
- use Aws\Multipart\AbstractUploader;
- use Aws\PhpHash;
- use Aws\ResultInterface;
- use GuzzleHttp\Psr7;
- use Psr\Http\Message\StreamInterface as Stream;
- use Aws\S3\Exception\S3MultipartUploadException;
- class MultipartUploader extends AbstractUploader
- {
- use MultipartUploadingTrait;
- const PART_MIN_SIZE = 5242880;
- const PART_MAX_SIZE = 5368709120;
- const PART_MAX_NUM = 10000;
-
- public function __construct(
- S3ClientInterface $client,
- $source,
- array $config = []
- ) {
- parent::__construct($client, $source, array_change_key_case($config) + [
- 'bucket' => null,
- 'key' => null,
- 'exception_class' => S3MultipartUploadException::class,
- ]);
- }
- protected function loadUploadWorkflowInfo()
- {
- return [
- 'command' => [
- 'initiate' => 'CreateMultipartUpload',
- 'upload' => 'UploadPart',
- 'complete' => 'CompleteMultipartUpload',
- ],
- 'id' => [
- 'bucket' => 'Bucket',
- 'key' => 'Key',
- 'upload_id' => 'UploadId',
- ],
- 'part_num' => 'PartNumber',
- ];
- }
- protected function createPart($seekable, $number)
- {
-
- $data = [];
-
- $config = $this->getConfig();
- $params = isset($config['params']) ? $config['params'] : [];
- foreach ($params as $k => $v) {
- $data[$k] = $v;
- }
- $data['PartNumber'] = $number;
-
- if ($seekable) {
-
- $body = $this->limitPartStream(
- new Psr7\LazyOpenStream($this->source->getMetadata('uri'), 'r')
- );
- } else {
-
- $source = $this->limitPartStream($this->source);
- $source = $this->decorateWithHashes($source, $data);
- $body = Psr7\Utils::streamFor();
- Psr7\Utils::copyToStream($source, $body);
- }
- $contentLength = $body->getSize();
-
- if ($contentLength === 0) {
- return false;
- }
- $body->seek(0);
- $data['Body'] = $body;
- if (isset($config['add_content_md5'])
- && $config['add_content_md5'] === true
- ) {
- $data['AddContentMD5'] = true;
- }
- $data['ContentLength'] = $contentLength;
- return $data;
- }
- protected function extractETag(ResultInterface $result)
- {
- return $result['ETag'];
- }
- protected function getSourceMimeType()
- {
- if ($uri = $this->source->getMetadata('uri')) {
- return Psr7\MimeType::fromFilename($uri)
- ?: 'application/octet-stream';
- }
- }
- protected function getSourceSize()
- {
- return $this->source->getSize();
- }
-
- private function decorateWithHashes(Stream $stream, array &$data)
- {
-
- $hash = new PhpHash('sha256');
- return new HashingStream($stream, $hash, function ($result) use (&$data) {
- $data['ContentSHA256'] = bin2hex($result);
- });
- }
- }
|