2012-11-16 26 views
6

我對新的谷歌驅動器API客戶端庫的文檔存在嚴重問題。看來這應該是一個很容易回答的問題,而不必把它放在stackoverflow上。我正在認真考慮在這一個上滾動我自己,一個64頁的「剛剛起作用」的庫到目前爲止是一個「令人頭痛的問題」Google Drive API - PHP客戶端庫 - 將uploadType設置爲可恢復的上傳

如何設置uploadType爲「可恢復」而不是默認「簡單」。我已經在圖書館搜尋了一個方法來做到這一點,但似乎並不存在。他們唯一的線索是其樣本上傳頁面上的代碼在這裏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', 
    )); 

沒有設置uploadType ... ???

他們的另一頁上docs只顯示uploadType的地址作爲GET的一部分:https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable但是當你用$service->files->insert,圖書館設置地址。

+2

所有這些類型的問題是可以避免的,如果谷歌只是有一組類似jQuery的記錄了他們的庫文件。如果他們至少有一個在線客戶端按類排序的php客戶端庫中提供的所有方法的列表,那將會非常棒。有這樣的事情存在嗎? – pathfinder

回答

5

下面的示例將與(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); 
} 
+0

這個工作完美,並且當另一個開發人員使用導致內存問題的簡單文件上傳器實現時非常有用。這個實現允許我們永遠不會超過700MB使用的內存,即使10個同時上傳2-5GB也是如此。優秀! – Stephan

5

這可能是一個較新的參考谷歌API的PHP客戶端的最新版本的工作,但這裏是谷歌在這個問題上的官方看法:https://developers.google.com/api-client-library/php/guide/media_upload

從文章:

可恢復的文件上傳

也可以跨多個請求拆分上載。這個 方便更大的文件,並且如果 有問題,允許恢復上傳。可恢復的上傳可以通過單獨的 元數據發送。

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

嗨安德魯,我使用這段代碼,但我不知道如何提出請求來獲取塊狀態來處理恢復中斷上載並顯示進度,此代碼僅在整個文件上傳時返回$狀態。 –

+0

嘿@RamtinGh;從我所知道的,'Google_Http_MediaFileUpload'類可以處理所有這些; 'nextChunk()'函數的源代碼會根據來自服務器的HTTP響應查看是否已經上傳完整的塊,如果是,則跳至下一個塊,直至遇到需要上載的塊: https://github.com/google/google-api-php-client/blob/master/src/Google/Http/MediaFileUpload.php#L128 –

+0

謝謝@Andrew,我知道客戶端庫處理這個,但使用這個代碼,除非整個文件被上傳,否則沒有辦法獲得塊狀態,類使用它來查看上傳是否完成。爲了恢復中斷的上傳,我需要請求查看發送了多少字節,庫本身不會執行此操作,如果連接中斷,腳本將停止執行,並且我不知道如何重新調用。 –

相關問題