Google Drive API - PHP Client Library - setting uploadType to resumable upload(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);
这篇关于Google Drive API - PHP 客户端库 - 将 uploadType 设置为可恢复上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Google Drive API - PHP 客户端库 - 将 uploadType 设置为可恢复上传
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01