我在使用新的Google云端硬盘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 作为地址的一部分显示为 GET: https://www.googleapis.com/upload/drive/v2/files ?uploadType=resumable
但是当您使用 $service->files->insert
时,库会设置地址。
以下示例将适用于最新版本的 Google 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);