|
8 | 8 | use AsyncAws\Core\RequestContext; |
9 | 9 | use AsyncAws\Core\Stream\FixedSizeStream; |
10 | 10 | use AsyncAws\Core\Stream\IterableStream; |
| 11 | +use AsyncAws\Core\Stream\ReadOnceResultStream; |
11 | 12 | use AsyncAws\Core\Stream\RequestStream; |
| 13 | +use AsyncAws\Core\Stream\RewindableStream; |
12 | 14 | use AsyncAws\Core\Stream\StringStream; |
13 | 15 |
|
14 | 16 | /** |
@@ -77,6 +79,11 @@ protected function buildBodyDigest(Request $request, bool $isPresign): string |
77 | 79 | if ($request->hasHeader('x-amz-content-sha256')) { |
78 | 80 | $hash = ((array) $request->getHeader('x-amz-content-sha256'))[0]; |
79 | 81 | } else { |
| 82 | + $body = $request->getBody(); |
| 83 | + if ($body instanceof ReadOnceResultStream) { |
| 84 | + $request->setBody($body = RewindableStream::create($body)); |
| 85 | + } |
| 86 | + |
80 | 87 | $hash = $request->getBody()->hash(); |
81 | 88 | } |
82 | 89 |
|
@@ -245,6 +252,13 @@ private function convertBodyToStream(Request $request, \DateTimeInterface $now, |
245 | 252 | $contentLength = $body->length(); |
246 | 253 | } |
247 | 254 |
|
| 255 | + // If content length is unknown, use the rewindable stream to read it once locally in order to get the length |
| 256 | + if (null === $contentLength) { |
| 257 | + $request->setBody($body = RewindableStream::create($body)); |
| 258 | + $body->read(); |
| 259 | + $contentLength = $body->length(); |
| 260 | + } |
| 261 | + |
248 | 262 | // no need to stream small body. It's simple to convert it to string directly |
249 | 263 | if ($contentLength < self::CHUNK_SIZE) { |
250 | 264 | $request->setBody($body = StringStream::create($body)); |
|
0 commit comments