pathfinder
pathfinder

Reputation: 1776

Google Drive API - PHP Client Library - setting uploadType to resumable upload

I am having serious issues with the documentation for the new google drive API client library. It seems this should be an easy one to answer without having to put it on stackoverflow. I am seriously considering rolling my own on this one, a 64 page library that "just works" is so far a "total headache"

How the heck do you set the uploadType to "resumable" instead of the default "simple". I have searched the library for a way to do this, but it seems non-existent. Their only hint is the code on their sample uploader page https://developers.google.com/drive/quickstart-php

//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = file_get_contents('document.txt');

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
    ));

Nothing here sets the uploadType...???

Their docs on another page just show uploadType as a part of the address as a GET: https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable but when you use $service->files->insert, the library sets the address.

Upvotes: 10

Views: 7899

Answers (2)

Andrew Odri
Andrew Odri

Reputation: 9422

This may be a newer reference, but here is Google's official take on this question: https://developers.google.com/api-client-library/php/guide/media_upload

From the article:

Resumable File Upload

It is also possible to split the upload across multiple requests. This is convenient for larger files, and allows resumption of the upload if there is a problem. Resumable uploads can be sent with separate metadata.

$file = new Google_Service_Drive_DriveFile();
$file->title = "Big File";
$chunkSizeBytes = 1 * 1024 * 1024;

// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->insert($file);

// Create a media file upload to represent our upload process.
$media = new Google_Http_MediaFileUpload(
  $client,
  $request,
  'text/plain',
  null,
  true,
  $chunkSizeBytes
);
$media->setFileSize(filesize("path/to/file"));

// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen("path/to/file", "rb");
while (!$status && !feof($handle)) {
  $chunk = fread($handle, $chunkSizeBytes);
  $status = $media->nextChunk($chunk);
 }

// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if($status != false) {
  $result = $status;
}

fclose($handle);
// Reset to the client to execute requests immediately in the future.
$client->setDefer(false);

Upvotes: 6

Chirag Shah
Chirag Shah

Reputation: 3674

The following sample will work with the latest version of the Google APIs PHP Client (https://code.google.com/p/google-api-php-client/source/checkout)

if ($client->getAccessToken()) {
  $filePath = "path/to/foo.txt";
  $chunkSizeBytes = 1 * 1024 * 1024;

  $file = new Google_DriveFile();
  $file->setTitle('My document');
  $file->setDescription('A test document');
  $file->setMimeType('text/plain');

  $media = new Google_MediaFileUpload('text/plain', null, true, $chunkSizeBytes);
  $media->setFileSize(filesize($filePath));

  $result = $service->files->insert($file, array('mediaUpload' => $media));

  $status = false;
  $handle = fopen($filePath, "rb");
  while (!$status && !feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $uploadStatus = $media->nextChunk($result, $chunk);
  }

  fclose($handle);
}

Upvotes: 6

Related Questions