【问题标题】:How to engage a Resumable upload to Google Drive using google-api-ruby client?如何使用 google-api-ruby 客户端将 Resumable 上传到 Google Drive?
【发布时间】:2012-09-14 10:28:39
【问题描述】:

我在将大型文件从我的 Linux 桌面上传到 GDrive 时遇到问题(通过使用不提供恢复甚至监控较大上传的 Grive)。

所以我想尝试使用 google-api-ruby 编写一个小脚本来完成这项工作。

随着 Google 推动文档讨论 可恢复上传https://developers.google.com/drive/manage-uploads#resumable – 我正在尝试使用 google-api-ruby 实现 可恢复上传并找到Google::APIClient::ResumableUploadclass。

示例对于理解这个 ruby​​ish API 的工作原理非常有用,但示例并没有提出可恢复上传的问题。

Google::APIClient::ResumableUploadclass ment 是否用于Resumable Uploads文档正在谈论,或者更确切地说是这个 Ruby 客户端提供的 conveicence 类?

Google::APIClient::ResumableUpload 构造函数使用 Google::APIClient::Result,当我在 Google::APIClient 实例上调用 executeexecute! 时,我得到了它。

据我了解,execute 方法仅适用于暴露于 Discovery API 的 Google API 方法(例如 drive.files.insert)。

不幸的是可恢复上传与以下 URI 相关:

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

并且似乎还没有成为发现的一部分,也没有集成到“更清洁”的 URI 方案中(为什么是 upload/drive 而不是 drive/upload?)。

是否可以为此 API URI 使用Google::APIClient 执行 方法以便将其与Google::APIClient::ResumableUpload 结合起来,或者我是否必须自己实现可恢复上传 (例如使用em-http-request)?

【问题讨论】:

    标签: ruby google-drive-api grive


    【解决方案1】:

    有关基本信息,请参阅files.insert 的示例。在该示例中,它使用的是多部分,但切换到可恢复是相当简单的。通常需要将uploadType 参数更改为“可恢复”。插入/更新的结果将包含对上传者的引用,您可以使用它来发送内容/检查是否完成。

    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)
    

    在即将发布的客户端库测试版中(现在任何一天 :),这将稍作更改,以便上传工作更加统一,无论协议如何。在大多数情况下,调用 execute() 就足够了,它会尝试上传文件。但是现有的方法仍然有效。你也可以通过调用来恢复:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 2020-03-10
      • 1970-01-01
      • 2020-11-03
      相关资源
      最近更新 更多