Google Drive API - PHP 客户端库 - 将 uploadType 设置为可恢复上传
我在使用新的 google drive API 客户端库的文档时遇到了严重问题.看起来这应该很容易回答,而不必将其放在 stackoverflow 上.我正在认真考虑推出我自己的这个,一个正常工作"的 64 页库到目前为止是一个令人头疼的问题"
I am having serious issues with the documentation for the new google drive API client library. It seems this should be an easy one to answer without having to put it on stackoverflow. I am seriously considering rolling my own on this one, a 64 page library that "just works" is so far a "total headache"
你到底是如何将uploadType 设置为resumable"而不是默认的simple"的.我已经在图书馆中搜索了一种方法来做到这一点,但它似乎不存在.他们唯一的提示是他们的示例上传页面上的代码 https://developers.google.com/drive/quickstart-php一个>
How the heck do you set the uploadType to "resumable" instead of the default "simple". I have searched the library for a way to do this, but it seems non-existent. Their only hint is the code on their sample uploader page 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...???
Nothing here sets the uploadType...???
他们在另一个页面上的 docs 只是将 uploadType 作为地址的一部分显示为 GET:https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable
但当你使用 $service->files->insert
时,库会设置地址.
Their docs on another page just show uploadType as a part of the address as a GET: https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable
but when you use $service->files->insert
, the library sets the address.
推荐答案
这可能是一个较新的参考,但这里是谷歌官方对这个问题的看法:https://developers.google.com/api-client-library/php/guide/media_upload
This may be a newer reference, but here is Google's official take on this question: https://developers.google.com/api-client-library/php/guide/media_upload
来自文章:
也可以跨多个请求拆分上传.这方便较大的文件,并允许恢复上传,如果有一个问题.可恢复的上传可以单独发送元数据.
Resumable File Upload
It is also possible to split the upload across multiple requests. This is convenient for larger files, and allows resumption of the upload if there is a problem. Resumable uploads can be sent with separate metadata.
$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);
相关文章