public function transfer() { $totalParts = (int) ceil($this->entityBody->getContentLength() / $this->partSize); $workers = min($totalParts, $this->options['concurrency']); $parts = $this->collectParts($workers); while ($this->transferState->count() < $totalParts) { $completedParts = $this->transferState->count(); $requests = array(); // Iterate over number of workers until total completed parts is what we need it to be for ($i = 0; $i < $workers && $completedParts + $i < $totalParts; $i++) { // Offset is the current pointer multiplied by the standard chunk length $offset = ($completedParts + $i) * $this->partSize; $parts[$i]->setOffset($offset); // If this segment is empty (i.e. buffering a half-full chunk), break the iteration if ($parts[$i]->getContentLength() == 0) { break; } // Add this to the request queue for later processing $requests[] = TransferPart::createRequest($parts[$i], $this->transferState->count() + $i + 1, $this->client, $this->options); } // Iterate over our queued requests and process them foreach ($this->client->send($requests) as $response) { // Add this part to the TransferState $this->transferState->addPart(TransferPart::fromResponse($response)); } } }
public function transfer() { while (!$this->entityBody->isConsumed()) { if ($this->entityBody->getContentLength() && $this->entityBody->isSeekable()) { // Stream directly from the data $body = new ReadLimitEntityBody($this->entityBody, $this->partSize, $this->entityBody->ftell()); } else { // If not-seekable, read the data into a new, seekable "buffer" $body = EntityBody::factory(); $output = true; while ($body->getContentLength() < $this->partSize && $output !== false) { // Write maximum of 10KB at a time $length = min(10 * Size::KB, $this->partSize - $body->getContentLength()); $output = $body->write($this->entityBody->read($length)); } } if ($body->getContentLength() == 0) { break; } $request = TransferPart::createRequest($body, $this->transferState->count() + 1, $this->client, $this->options); $response = $request->send(); $this->transferState->addPart(TransferPart::fromResponse($response)); } }