Google Drive API-PHP客户端库-将uploadType设置为可恢复上载
我对新的google drive API客户端库的文档有严重的问题。似乎这应该是一个简单的答案,而不必把它放在stackoverflow上。我正在认真考虑在这本书上发表自己的观点,一本64页的“刚刚好用”的图书馆到目前为止是一本“令人头痛的书” 你是如何将uploadType设置为“resumable”而不是默认的“simple”的呢。我在图书馆里搜寻过一种方法,但似乎不存在。他们唯一的提示是示例上传页面上的代码 此处没有设置上载类型Google Drive API-PHP客户端库-将uploadType设置为可恢复上载,php,google-drive-api,google-api-php-client,Php,Google Drive Api,Google Api Php Client,我对新的google drive API客户端库的文档有严重的问题。似乎这应该是一个简单的答案,而不必把它放在stackoverflow上。我正在认真考虑在这本书上发表自己的观点,一本64页的“刚刚好用”的图书馆到目前为止是一本“令人头痛的书” 你是如何将uploadType设置为“resumable”而不是默认的“simple”的呢。我在图书馆里搜寻过一种方法,但似乎不存在。他们唯一的提示是示例上传页面上的代码 此处没有设置上载类型 他们在另一个页面上只是将uploadType作为地址的一部分
他们在另一个页面上只是将uploadType作为地址的一部分显示为GET:
?uploadType=resumable
,但是当您使用$service->files->insert
时,库会设置地址。以下示例将与最新版本的Google API PHP客户端()
这可能是一个较新的参考,但以下是谷歌对这个问题的官方看法: 从文章中: 可恢复文件上载 还可以将上载拆分为多个请求。这 对于较大的文件很方便,并允许在以下情况下恢复上载: 有一个问题。可恢复的上传可以单独发送 元数据
$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有一套类似于jQuery文档库的文档,那么所有这些类型的问题都可以避免。如果他们至少有一个php客户端库中所有可用方法的列表,并在网上的某个地方按类排序,那就太棒了。类似的东西存在吗?嗨,安德鲁,我正在使用这段代码,但我不知道我应该如何请求获取区块状态来处理恢复中断的上传并显示进度,这段代码只有在上传整个文件时才会返回$status。嘿@RamtinGh;据我所知,
Google\uhttp\umediafileupload
类为您处理所有这些;nextChunk()
函数的源代码显示,根据服务器的HTTP响应,是否已上载完整的块,如果已上载,它将跳过到下一个块,直到找到需要上载的块:Thank@Andrew,我知道客户端库会处理此问题,但使用此代码,除非上传整个文件,否则无法获取区块状态,类使用它查看上传是否完成。要恢复中断的上载,我需要发出一个请求,查看发送了多少字节,库本身不会这样做,如果连接中断,脚本将停止执行,我不知道如何重新mue。我发布了一个问题,请看一看:当另一个开发人员使用一个简单的文件上传器实现时,这非常有用,会导致内存问题。这种实现允许我们永远不会超过700MB的内存使用,即使同时上传10次2-5GB。杰出的
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);
}
$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);