Google\Cloud\Upload\ResumableUploader::upload PHP Method

upload() public method

Triggers the upload process.
public upload ( ) : array
return array
    public function upload()
    {
        $rangeStart = $this->rangeStart;
        $response = null;
        $resumeUri = $this->getResumeUri();
        $size = $this->data->getSize() ?: '*';
        do {
            $data = new LimitStream($this->data, $this->chunkSize ?: -1, $rangeStart);
            $rangeEnd = $rangeStart + ($data->getSize() - 1);
            $headers = ['Content-Length' => $data->getSize(), 'Content-Type' => $this->contentType, 'Content-Range' => "bytes {$rangeStart}-{$rangeEnd}/{$size}"];
            $request = new Request('PUT', $resumeUri, $headers, $data);
            try {
                $response = $this->requestWrapper->send($request, $this->requestOptions);
            } catch (GoogleException $ex) {
                throw new GoogleException("Upload failed. Please use this URI to resume your upload: {$this->resumeUri}", $ex->getCode());
            }
            $rangeStart = $this->getRangeStart($response->getHeaderLine('Range'));
        } while ($response->getStatusCode() === 308);
        return json_decode($response->getBody(), true);
    }

Usage Example

 /**
  * @expectedException Google\Cloud\Exception\GoogleException
  */
 public function testThrowsExceptionWithFailedUpload()
 {
     $resumeUriResponse = new Response(200, ['Location' => 'theResumeUri']);
     $this->requestWrapper->send(Argument::which('getMethod', 'POST'), Argument::type('array'))->willReturn($resumeUriResponse);
     $this->requestWrapper->send(Argument::which('getMethod', 'PUT'), Argument::type('array'))->willThrow('Google\\Cloud\\Exception\\GoogleException');
     $uploader = new ResumableUploader($this->requestWrapper->reveal(), $this->stream, 'http://www.example.com');
     $uploader->upload();
 }