如何使用google-api-ruby客户端将可恢复上传内容加入Google云端硬盘? [英] How to engage a Resumable upload to Google Drive using google-api-ruby client?

查看:181
本文介绍了如何使用google-api-ruby客户端将可恢复上传内容加入Google云端硬盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我的Linux桌面上的大文件上传到 GDrive (通过使用Grive,它不提供恢复功能,甚至无法监视更大的上传文件)。
$ b $因此,我试图用 google-api-ruby 编写一个小脚本来完成这项工作。



由于Google驱动器文档讨论了可恢复的上传 - https://developers.google。 com / drive / manage-uploads#resumable - 我试图使用 google-api-ruby 实现可恢复的上传,并找到 Google :: APIClient :: ResumableUpload 类。

样本对理解这个rubyish API的工作原理非常有用,但是这些样本没有提及可恢复上传的问题。 em>可恢复的上传文档正在讨论这个Ruby客户端提供的conveicence类吗?


$ b

Google :: APIClient: :当我调用 execute 时,ResumableUpload 构造函数消耗了一个 Google :: APIClient :: Result $ c>或执行! Google :: APIClient 实例上。



就我所知, execute 方法只适用于暴露给 Discovery的Google API方法API (例如 drive.files.insert )。

不幸可恢复的上传绑定到以下URI:


https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable




,但似乎并未成为 Discovery 的一部分,或者被整合到一个'更清晰的'URI模式中(为什么 upload / drive 而不是驱动器/上传?)。

是否可以使用<$ c为此API URI添加$ c> Google :: APIClient execute 方法,以便将其与 Google :: APIClient :: ResumableUpload oder是否必须自己实现可恢复的上传(例如,通过使用 em-http-request )?

解决方案

请参阅 files.insert 作为基础。在那个例子中,它使用多部分,但切换到可恢复是相当直接的。大多需要将uploadType参数更改为可恢复。插入/更新的结果将包含对上传器的引用,而不是您可以用来发送内容/检查以完成的内容。

  media = Google :: APIClient :: UploadIO.new(file_name,mime_type)
result = client.execute(
:api_method => drive.files.insert,
:body_object => ;文件,
:media =>媒体,
:parameters => {
'uploadType'=>'resumable',
'alt'=>'json '))

#发送内容
result.resumable_upload.send_all(客户端)

在即将发布的客户端库beta版中(现在的任何一天),这会稍微改变,以便上传不管协议如何均匀一致。在大多数情况下前进,调用execute()就足够了,它会尝试上传文件。但现有方法仍然有效。你也可以通过调用恢复:

  if!result.resumable_upload.complete? 
client.execute(result.resumable_upload)#继续发送...
结束

示例不显示错误处理,但您可以检查result.resumable_upload.complete?或.expired?检查状态。不完整的可恢复上传会在一段时间不活动后过期(大约一小时左右)。如果过期,您需要从头开始重新启动。


I having issues uploading large files form my Linux Desktop to GDrive (by using Grive which does not offer recovery or even monitoring of larger uploads).

So I came up trying to use google-api-ruby to write a small script doing the job.

As Google drives documentation talks about Resumable uploadshttps://developers.google.com/drive/manage-uploads#resumable – I was trying to implement Resumable uploads using google-api-ruby and found Google::APIClient::ResumableUploadclass.

Samples are quite useful to understand the principle of how this rubyish API works, but the samples don't broach the issuse of Resumable uploads.

Is Google::APIClient::ResumableUploadclass ment to be used for Resumable uploads the documenation is talking about or rather a conveicence class provided by this Ruby client?

Google::APIClient::ResumableUpload constructor consumes a Google::APIClient::Resultwhich I get, when I call execute or execute! on a Google::APIClient instance.

As far as I understand execute method will only work on Google API methods which are exposed to Discovery API (such as drive.files.insert).

Unfortunalty Resumable uploads are tied to following URI:

https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable

and does not seem to be part of Discovery yet or integegrated into a 'cleaner' URI scheme (why upload/drive instead of drive/upload?).

Is it possible to use Google::APIClient execute methods for this API URI in order to combine it with Google::APIClient::ResumableUpload oder do I have to implement Resumable uploads by myself (e.g. by using em-http-request)?

解决方案

See the example for files.insert for the basics. In that example, it is using multipart, but switching to resumable is fairly straightforward. Mostly requires changing the uploadType parameter to 'resumable'. The result from the insert/update will contain a reference to the uploader than you can use to send the content/check for completion.

media = Google::APIClient::UploadIO.new(file_name, mime_type)
result = client.execute(
  :api_method => drive.files.insert,
  :body_object => file,
  :media => media,
  :parameters => {
    'uploadType' => 'resumable',
    'alt' => 'json'})

# Send content
result.resumable_upload.send_all(client)

In the upcoming beta release of the client library (any day now :), this will change slightly so that uploads work a little more uniformly regardless of protocol. Going forward in most cases, calling execute() will be sufficient and it will attempt to upload the file. But the existing method will still work. You'll also be able resume by just calling:

if !result.resumable_upload.complete?
   client.execute(result.resumable_upload) # Continue sending... 
end

Example doesn't show the error handling, but you can check result.resumable_upload.complete? or .expired? to check the status. Incomplete resumable uploads expire after a period of inactivity (on the order of an hour or so.) If expired, you'll need to restart from the beginning.

这篇关于如何使用google-api-ruby客户端将可恢复上传内容加入Google云端硬盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆