| 1 |
<?php |
| 2 |
namespace Aws\S3; |
| 3 |
|
| 4 |
use Aws\HashingStream; |
| 5 |
use Aws\Multipart\AbstractUploader; |
| 6 |
use Aws\PhpHash; |
| 7 |
use Aws\ResultInterface; |
| 8 |
use GuzzleHttp\Psr7; |
| 9 |
use Psr\Http\Message\StreamInterface as Stream; |
| 10 |
use Aws\S3\Exception\S3MultipartUploadException; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class MultipartUploader extends AbstractUploader |
| 16 |
{ |
| 17 |
use MultipartUploadingTrait; |
| 18 |
|
| 19 |
const PART_MIN_SIZE = 5242880; |
| 20 |
const PART_MAX_SIZE = 5368709120; |
| 21 |
const PART_MAX_NUM = 10000; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
public function __construct( |
| 66 |
S3ClientInterface $client, |
| 67 |
$source, |
| 68 |
array $config = [] |
| 69 |
) { |
| 70 |
parent::__construct($client, $source, array_change_key_case($config) + [ |
| 71 |
'bucket' => null, |
| 72 |
'key' => null, |
| 73 |
'exception_class' => S3MultipartUploadException::class, |
| 74 |
]); |
| 75 |
|
| 76 |
if ($this->displayProgress) { |
| 77 |
$this->getState()->setProgressThresholds($this->source->getSize()); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
protected function loadUploadWorkflowInfo() |
| 82 |
{ |
| 83 |
return [ |
| 84 |
'command' => [ |
| 85 |
'initiate' => 'CreateMultipartUpload', |
| 86 |
'upload' => 'UploadPart', |
| 87 |
'complete' => 'CompleteMultipartUpload', |
| 88 |
], |
| 89 |
'id' => [ |
| 90 |
'bucket' => 'Bucket', |
| 91 |
'key' => 'Key', |
| 92 |
'upload_id' => 'UploadId', |
| 93 |
], |
| 94 |
'part_num' => 'PartNumber', |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
protected function createPart($seekable, $number) |
| 99 |
{ |
| 100 |
|
| 101 |
$data = []; |
| 102 |
|
| 103 |
|
| 104 |
$config = $this->getConfig(); |
| 105 |
$params = isset($config['params']) ? $config['params'] : []; |
| 106 |
foreach ($params as $k => $v) { |
| 107 |
$data[$k] = $v; |
| 108 |
} |
| 109 |
|
| 110 |
$data['PartNumber'] = $number; |
| 111 |
|
| 112 |
|
| 113 |
if ($seekable) { |
| 114 |
|
| 115 |
$body = $this->limitPartStream( |
| 116 |
new Psr7\LazyOpenStream($this->source->getMetadata('uri'), 'r') |
| 117 |
); |
| 118 |
} else { |
| 119 |
|
| 120 |
$source = $this->limitPartStream($this->source); |
| 121 |
$source = $this->decorateWithHashes($source, $data); |
| 122 |
$body = Psr7\Utils::streamFor(); |
| 123 |
Psr7\Utils::copyToStream($source, $body); |
| 124 |
} |
| 125 |
|
| 126 |
$contentLength = $body->getSize(); |
| 127 |
|
| 128 |
|
| 129 |
if ($contentLength === 0) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
$body->seek(0); |
| 134 |
$data['Body'] = $body; |
| 135 |
|
| 136 |
if (isset($config['add_content_md5']) |
| 137 |
&& $config['add_content_md5'] === true |
| 138 |
) { |
| 139 |
$data['AddContentMD5'] = true; |
| 140 |
} |
| 141 |
|
| 142 |
$data['ContentLength'] = $contentLength; |
| 143 |
|
| 144 |
return $data; |
| 145 |
} |
| 146 |
|
| 147 |
protected function extractETag(ResultInterface $result) |
| 148 |
{ |
| 149 |
return $result['ETag']; |
| 150 |
} |
| 151 |
|
| 152 |
protected function getSourceMimeType() |
| 153 |
{ |
| 154 |
if ($uri = $this->source->getMetadata('uri')) { |
| 155 |
return Psr7\MimeType::fromFilename($uri) |
| 156 |
?: 'application/octet-stream'; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
protected function getSourceSize() |
| 161 |
{ |
| 162 |
return $this->source->getSize(); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private function decorateWithHashes(Stream $stream, array &$data) |
| 174 |
{ |
| 175 |
|
| 176 |
$hash = new PhpHash('sha256'); |
| 177 |
return new HashingStream($stream, $hash, function ($result) use (&$data) { |
| 178 |
$data['ContentSHA256'] = bin2hex($result); |
| 179 |
}); |
| 180 |
} |
| 181 |
} |