10

I am trying to use the Google Drive API via their PHP Client Library to upload a large file. Currently it fails because the only method seemingly available is the "simple" upload method. Unfortunately that requires you to load the entire file as data and it hits my php memory limit. I would like to know if it is possible and see an example of how it is done. I know there is a method for resumable and chuncked uploads, but there is no documentation on how to use it.

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$client = new Google_Client();
// Get your credentials from the APIs Console
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

//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',
    ));

print_r($createdFile);
?>
pathfinder
  • 1,606
  • 18
  • 22
  • Large is a relative term. Is it 10mb? 1gb? 10gb? – Jay Lee Dec 31 '12 at 13:30
  • does not matter to answer the question. But a 10mb file is large enough to need the solution... A 5mb generally makes the "simple" upload fail. I am needing to upload files between 50mb and 1gb generally. – pathfinder Dec 31 '12 at 17:48
  • 3
    I had the same problem until I found the code for a multipart (resumable) upload, shown on this answer: http://stackoverflow.com/a/14693917/1060686. It should allow you to upload big files within the usual script memory constrains (You can use the same method for downloads, in case you are interested. Also, make sure you are using the last version of the php client API [https://code.google.com/p/google-api-php-client/source/checkout ] and $apiConfig['use_objects'] is set to false on config.php) [1]: [2]: – NotGaeL Mar 10 '13 at 11:32

1 Answers1

0

Looks like this has been answered already? Read the file as a stream, one chunk at a time and add that to a buffer/write it straight to the socket:

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

Community
  • 1
  • 1
Mark
  • 604
  • 5
  • 8