Ram*_* Gh 13 php file-upload google-drive-api
我正在使用PHP客户端库(Beta)来使用谷歌驱动器api,到目前为止,我可以在chuncks中授权和上传文件.根据文档,应该采取这三个步骤来上传文件:
- 开始一个可恢复的会话.
- 保存可恢复的会话URI.
- 上传文件.
我认为客户端库处理.同样,根据文档,如果我想显示进度或恢复中断上传,或者为了处理错误,我需要捕获响应并且还能够发送如下请求:
> PUT {session_uri} HTTP/1.1 Content-Length: 0 Content-Range: bytes
> */2000000
Run Code Online (Sandbox Code Playgroud)
但我不知道我应该如何提出这样的请求,我在哪里可以得到响应,我用来上传的php代码,就像任何其他PHP代码一样,只有在完成执行时返回值,即上传时已经完成了.这是我用来上传文件的功能(Resumable):
function uploadFile($service,$client,$filetoUpload,$parentId){
$file = new Google_Service_Drive_DriveFile();
$file->title = $filetoUpload['name'];
$chunkSizeBytes = 1 * 1024 * 1024;
// Set the parent folder.
if ($parentId != null) {
$parent = new Google_Service_Drive_ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
}
// 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,
$filetoUpload['type'],
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($filetoUpload['tmp_name']));
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen($filetoUpload['tmp_name'], "rb");
while (!$status && !feof($handle)) {
set_time_limit(120);
$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;
set_time_limit(30);
echo "<pre>";
print_r($result);
}
fclose($handle);
// Reset to the client to execute requests immediately in the future.
$client->setDefer(false);
}
Run Code Online (Sandbox Code Playgroud)
我应该制作一个单独的php文件来处理这些请求吗?如果是的话,应该如何判断我要求的文件状态?谢谢.
显然,BETA客户端 API 根本不支持恢复上传。\n请在 Github 上查看此问题,它要求修复此问题。当然,应该很容易修改类(见下文)并创建拉请求,以在提供会话 URL 时支持恢复现有上传。
\n\n但是,有一种简单的方法可以在上传块后获取进度。\n-object Google_Http_MediaFileUpload($media在您的示例中)有一个名为“getProgress”的公共方法,可以随时调用该方法。\n(请注意查看 API-客户端-库的源代码)。
为了获取上传状态,我添加了一个参数来通过调整块大小来修改进度精度。由于使用的块越多,产生的协议开销就越大,因此应避免将精度设置得尽可能低。
\n\n因此,您可以如下修改源代码以在每个块之后输出进度:
\n\nfunction uploadFile($service,$client,$filetoUpload,$parentId,$progressPrecision = 1){\n$file = new Google_Service_Drive_DriveFile();\n$file->title = $filetoUpload[\'name\'];\n$filesize = filesize($filetoUpload[\'tmp_name\']);\n//minimum chunk size needs to be 256K\n$chunkSizeBytes = min( $filesize / 100 * $progressPrecision, 262144);\n\n// Set the parent folder.\n if ($parentId != null) {\n $parent = new Google_Service_Drive_ParentReference();\n $parent->setId($parentId);\n $file->setParents(array($parent));\n }\n\n// Call the API with the media upload, defer so it doesn\'t immediately return.\n$client->setDefer(true);\n$request = $service->files->insert($file);\n\n// Create a media file upload to represent our upload process.\n$media = new Google_Http_MediaFileUpload(\n $client,\n $request,\n $filetoUpload[\'type\'],\n null,\n true,\n $chunkSizeBytes\n);\n$media->setFileSize($filesize);\nRun Code Online (Sandbox Code Playgroud)\n\n\xe2\x80\xa6
\n\nwhile (!$status && !feof($handle)) {\n set_time_limit(120); \n $chunk = fread($handle, $chunkSizeBytes);\n $status = $media->nextChunk($chunk);\n if(!$status){ //nextChunk() returns \'false\' whenever the upload is still in progress\n echo \'sucessfully uploaded file up to byte \' . $media->getProgress() . \n \' which is \' . ( $media->getProgress() / $chunkSizeBytes ) . \'% of the whole file\';\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n希望这可以帮助。我会看看是否能找到一些时间向客户端库添加简历支持。
\n\n编辑:根据此文档,块至少需要 256KB 大。更改了代码。
\n\nEDIT2:我刚刚添加了一个拉取请求来添加恢复功能。如果被拒绝,您仍然可以决定是否可以修改/扩展客户端。如果它被接受,只需将返回值存储$media->getResumeUri()在数据库中,然后$media->resume($previously_stored_return_value)在实例化后调用以恢复该过程。
| 归档时间: |
|
| 查看次数: |
1228 次 |
| 最近记录: |