我有新谷歌驱动API客户端库的文档的严重问题。 看来这应该是轻松的一年,而不必把它放在计算器来回答。 我很认真地考虑我自己的滚动在这其中,64页库,“只是工程”是迄今为止一个“总头疼”
赫克你怎么设置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 ... ???
他们的文档另一页上只显示uploadType的地址作为GET的一部分: https://www.googleapis.com/upload/drive/v2/files ?uploadType=resumable
,但是当你使用$service->files->insert
,库设置地址。
下面的示例将与谷歌API的PHP客户端的最新版本的工作( 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);
}
这可能是一个较新的基准,但这里是谷歌在这个问题上采取官方: 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);
文章来源: Google Drive API - PHP Client Library - setting uploadType to resumable upload