我对新的Google Drive API客户端库的文档存在严重问题。看起来这应该是一个容易回答,而不必把它放在stackoverflow上。我正在认真考虑在这个问题上滚动自己,一个64页的“正常工作”的图书馆到目前为止一直是“头痛”
如何将uploadType设置为“resumable”而不是默认的“simple”。我在图书馆搜索了一种方法,但它似乎不存在。他们唯一的提示是他们的样本上传页面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
时,该库会设置地址
答案 0 :(得分:6)
以下示例适用于最新版本的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);
}
答案 1 :(得分:5)
这可能是较新的参考,但这是Google对此问题的正式看法: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);