Google Drive Resumable Upload

982 views
Skip to first unread message

Christiano Becker

unread,
Feb 2, 2013, 1:17:45 PM2/2/13
to google-api...@googlegroups.com
I'm trying to upload some files to Google Drive. The files have 200MB or more, so I'm using resumable upload.
$local_path = '/opt/backup/';
$f_name = 'backup.bz2';
$file = new Google_DriveFile();
$file->setTitle($f_name);
$file->setDescription($f_name);
$file->setMimeType('binary/octet-stream');
$file_result = $google_drive->files->insert($file, array('data' => file_get_contents($local_path . $f_name), 'mimeType' => 'binary/octet-stream', 'uploadType' => 'resumable'));

I get no errors and no file uploaded to Google Drive. If I change de uploadType to 'media' or 'multipart', everything goes OK, but the file must be 5MB or less.

I've tried using Latest SVN and 0.6.0 on Mac (10.8.2 + php5.3.21 via ports) and Linux (Debian 6).

Thanks

Rudy Preston

unread,
Feb 2, 2013, 1:25:05 PM2/2/13
to google-api-php-client
I have been trying the same thing and when I used it like you have posted I ran into php memory limits as set in the ini file.
file_get_contents tries to read the whole file into memory and it was larger than the 90 mb of memory allowed for php.

I have yet to find a way to do this successfully.

**--Rudy--**
Mountain - 928-864-7968 
Valley - 480.382.5288
pathf...@ethos7.com
ru...@ethos7.com

Simplicity is the ultimate sophistication
--
Leonardo da Vinci



--
You received this message because you are subscribed to the Google Groups "google-api-php-client" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-api-php-c...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Christiano Becker

unread,
Feb 2, 2013, 7:38:48 PM2/2/13
to google-api...@googlegroups.com
But even using small files, and I mean with 9KB, nothing happens.
Now I believe that is something with the google-api-php-client . For a simple test, I added a var_dump($request->getUrl()) in the Google_CurlIO::makeRequest() function.
When I set de upload type to media, I got:
Uploading file: backup_2011-01-02_06-00.sql.bz2

But when I just change the upload type to resumable, I got nothing. The script just jump to the next file upload until the script end.

Seems that the "resumable" upload type are not being treated correctly, since no request are made.
Or the "resumable" uses other class/function to the request?

Att.
Christiano Becker
To unsubscribe from this group and stop receiving emails from it, send an email to google-api-php-client+unsub...@googlegroups.com.

Rudy Preston

unread,
Feb 2, 2013, 8:13:29 PM2/2/13
to google-api-php-client
Yeay, I think the resumable upload is not working any way you try and I have yet to find a way using the API


$file_result = $google_drive->files->insert($file, array('data' => file_get_contents($local_path . $f_name), 'mimeType' => 'binary/octet-stream', 'uploadType' => 'resumable'));


I do not think it is possible to set uploadType in this way.


I have had this issue up on stackoverflow.com for a month or more and nobody responded except a google employee paid to stalk the website tagging everyone's questions. And all he said was I should check the docs on google code which were of no use whatsoever. I told him their docs were totally lacking any sort of help and he deleted his answer.

http://stackoverflow.com/questions/13808528/upload-large-file-to-google-drive-with-php-client-library#comment19514568_13808528

http://stackoverflow.com/questions/13413036/google-drive-api-php-client-library-setting-uploadtype-to-resumable-upload

**--Rudy--**
Mountain - 928-864-7968 
Valley - 480.382.5288
pathf...@ethos7.com
ru...@ethos7.com

Simplicity is the ultimate sophistication
--
Leonardo da Vinci



To unsubscribe from this group and stop receiving emails from it, send an email to google-api-php-c...@googlegroups.com.

Christiano Becker

unread,
Feb 2, 2013, 9:07:20 PM2/2/13
to google-api...@googlegroups.com
I got it!!!!!! After reading 100000000 times the API code, I did using this code (just need to do a better one, but it works!):

// Create a New Google Drive File
$file = new Google_DriveFile();
$file->setTitle($file_name);
$file->setDescription($file_name);
$file->setMimeType('binary/octet-stream');

// Set the Parent Folder on Google Drive
$parent = new Google_ParentReference();
$parent->setId($parent_id);
$file->setParents(array($parent));

// Now here is the trick: Create a Google Media File Upload, you will use this to "activate" the upload
// The parameters are: mimeType, data, resumable (true), and the chunk size (I set in this case 2MB, de default are 256KB) 
$media = new Google_MediaFileUpload('binary/octet-stream', file_get_contents($local_path . $f_nome), true, (2048*1024));

$media->progress = 0;
$media->mimeType = 'binary/octet-stream';

// Now, you call the insert and pass de mediaUpload parameter as your $media
$file_request = $this->google_drive->files->insert($file, array('mimeType' => 'binary/octet-stream', 'mediaUpload' => $media));

// Now you do the upload
// While the upload are not complete, the $media->nextChunk() will always return false.
// When the upload are completed, will return an array with informations about the file uploaded
// You can verify the progress (int) $media->progress , the size (int) $media->size and the chunk (int) -> $media->chunkSize
$upload_result = false;
while ($upload_result === false) {
$upload_result = $media->nextChunk($file_request);
}

And be happy!!! :D
Hope this can be useful for others!

Att.
Christiano Becker

Rudy Preston

unread,
Feb 2, 2013, 9:17:23 PM2/2/13
to google-api-php-client
I will test it on my app tonight and let you know... thank you so much for this... I hop it works :)

**--Rudy--**
Mountain - 928-864-7968 
Valley - 480.382.5288
pathf...@ethos7.com
ru...@ethos7.com

Simplicity is the ultimate sophistication
--
Leonardo da Vinci



--

Chirag Shah

unread,
Feb 3, 2013, 12:28:34 AM2/3/13
to google-api...@googlegroups.com
If you want to loading the entire file in memory before uploading, you can stream the data using the latest version of the client library.

Here's an example for youtube:
if ($client->getAccessToken()) {
  $videoPath = "path/to/foo.mp4";
  $snippet = new Google_VideoSnippet();
  $snippet->setTitle("Test title2");

  $status = new Google_VideoStatus();
  $status->privacyStatus = "private";

  $video = new Google_Video();
  $video->setSnippet($snippet);
  $video->setStatus($status);

  $chunkSizeBytes = 1 * 1024 * 1024;
  $media = new Google_MediaFileUpload('video/mp4', null, true, $chunkSizeBytes);
  $media->setFileSize(filesize($videoPath));

  $result = $youtube->videos->insert("status,snippet", $video,
      array('mediaUpload' => $media));

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

  fclose($handle);
}

Rudy Preston

unread,
Feb 3, 2013, 12:48:07 AM2/3/13
to google-api-php-client
Thanks Chirag,

Do you have a working example for google drive?

According to the google drive docs resumable uploads are the "recommended" way to upload and there is no example of how to do this anywhere. It would be great to have a resumable example on this page for all the APIs:

https://developers.google.com/drive/v2/reference/files/insert

and maybe it would be really important if the best practice is to use the resumable upload that an example of this be placed on the quickstart page as well:

https://developers.google.com/drive/quickstart-php

Hundreds of people have viewed my stackoverflow.com questions regarding this but nobody has found an answer. If you post one, I will certainly go update my stackoverflow questions to have an answer.

Rudy

**--Rudy--**
Mountain - 928-864-7968 
Valley - 480.382.5288
pathf...@ethos7.com
ru...@ethos7.com

Simplicity is the ultimate sophistication
--
Leonardo da Vinci



Christiano Becker

unread,
Feb 5, 2013, 11:08:51 AM2/5/13
to google-api...@googlegroups.com
I did some upload tests and verified that sometimes the file on Google Drive are corrupted. So, just a tip, is verify the md5 checksum after the upload completed, and if the checksums are different (local and remote) you can re-upload.
$media->nextChunk() returns and array that contains the md5 checksum on Google Drive, and you can use the php's function md5_file() to get the check for the local file.

Att
Christiano Becker
To unsubscribe from this group and stop receiving emails from it, send an email to google-api-php-client+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "google-api-php-client" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-api-php-client+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "google-api-php-client" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-api-php-client+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages