2012-12-10 51 views
10

我想通過他們的PHP客戶端庫使用Google Drive API上傳大文件。目前它失敗了,因爲唯一可用的方法是「簡單」上傳方法。不幸的是,這需要你加載整個文件作爲數據,它符合我的PHP內存限制。我想知道是否有可能並看到如何完成的例子。我知道有一種可恢復和分塊上傳的方法,但沒有關於如何使用它的文檔。用PHP客戶端庫將大文件上傳到谷歌驅動器

<?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); 
?> 
+0

大是相對而言的。是10mb嗎? 1GB? 10GB? –

+0

無關緊要地回答這個問題。但一個10MB的文件足夠大,需要解決方案......一個5MB通常會導致「簡單」上傳失敗。我需要一般上傳50mb到1gb之間的文件。 – pathfinder

+3

我有同樣的問題,直到我找到多部分(可恢復)上載的代碼,顯示在這個答案:http://stackoverflow.com/a/14693917/1060686。它應該允許您在通常的腳本內存限制內上傳大文件(如果您有興趣,可以使用相同的方法進行下載。另外,請確保您使用的是最新版本的PHP客戶端API [https:// code.google.com/p/google-api-php-client/source/checkout]和$ apiConfig ['use_objects']在config.php上設置爲false) [1]: [2]: – NotGaeL

回答

相關問題