我在 Rails 应用程序中使用 google-api-ruby-client 与 Google Drive 交互。所有基本功能,如在文件夹中列出文件、获取、移动、复制和删除文件、创建新文件夹等都运行良好。
但是,上传新文件总是因 Timeout::Error 而失败
我从我的网站(多部分/表单数据)收到要作为常规文件上传的文件,这就是我将其上传到 Google 驱动器的方式
result = nil
new_file_obj = google_drive.files.insert.request_schema.new({
'title' => file_name,
'mimeType' => file_mime_type,
'parents' => [{'id' => current_folder_id}]
})
file_content = Google::APIClient::UploadIO.new(new_file.tempfile, file_mime_type, file_name)
result = google_client.execute(
api_method: google_drive.files.insert,
body_object: new_file_obj,
media: file_content,
parameters: {
'uploadType' => 'multipart',
'alt' => 'json'
}
)
这new_file
是客户端上传的文件。new_file.tempfile
给出一个类型的对象Tempfile
。
该execute
方法永远不会返回,最终我得到一个 Timeout::Error 异常。这是相关的堆栈跟踪:
/lib/ruby/1.9.1/net/protocol.rb:140:in `rescue in rbuf_fill'
/lib/ruby/1.9.1/net/protocol.rb:134:in `rbuf_fill'
/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil'
/lib/ruby/1.9.1/net/protocol.rb:126:in `readline'
/lib/ruby/1.9.1/net/http.rb:2211:in `read_status_line'
/lib/ruby/1.9.1/net/http.rb:2200:in `read_new'
/lib/ruby/1.9.1/net/http.rb:1183:in `transport_request'
/lib/ruby/1.9.1/net/http.rb:1169:in `request'
/lib/ruby/1.9.1/net/http.rb:1162:in `block in request'
/lib/ruby/1.9.1/net/http.rb:627:in `start'
/lib/ruby/1.9.1/net/http.rb:1160:in `request'
/lib/ruby/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:74:in `perform_request'
/lib/ruby/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:37:in `call'
/lib/ruby/gems/1.9.1/gems/faraday-0.8.4/lib/faraday/request/url_encoded.rb:14:in `call'
/lib/ruby/gems/1.9.1/gems/google-api-client-0.5.0/lib/google/api_client/request.rb:154:in `send'
/lib/ruby/gems/1.9.1/gems/google-api-client-0.5.0/lib/google/api_client.rb:546:in `execute'
我按照此处的示例编写了此代码:https ://developers.google.com/drive/examples/ruby#saving_new_files我在这里缺少什么?
我要上传的文件是一个小的 png 图像。该文件正确地进入我的 Web 应用程序,因为如果我将它写入磁盘,我可以查看该文件。谷歌服务器肯定没有关闭,因为我可以从 drive.google.com 上传文件。这也意味着我的网络足够好。
那么究竟是什么导致超时?
提到了增加 read_timeout(http://stackoverflow.com/questions/10011387/rescue-in-rbuf-fill-timeouterror-timeouterror)的相同异常的解决方案。那是我应该做的吗?如果是这样,我该如何使用 google-api-ruby-client sdk 在此处进行操作?