Ruby中下载的Google文档的路径在哪里?

时间:2017-02-11 03:09:30

标签: ruby-on-rails ruby google-api-client

我试图用Ruby下载google文档文件。我无法找到下载文件的路径。我命令用'export_file' ruby中的方法如下:

file_id =' xxxxxx' content = google_drive.export_file(file_id,                                     '应用/ PDF&#39 ;,                                     download_dest:StringIO.new)

控制台中的响应如下所示。

写块(1389字节) 写块(1389字节) 写块(1389字节) 写块(1389字节) 写块(896字节) 写块(2个字节) 写块(1234字节) 成功 - #

=> #

似乎已经下载了一些东西但是在轨道中,我无法找到下载文件的路径。我试图用名称(我在Google文档中看到的名称)搜索文件是徒劳的。

如何访问下载的文件?我如何访问"内容"?请与我分享您的见解!

最佳

2 个答案:

答案 0 :(得分:1)

这只会在StringIo对象中添加字符串,在完成后需要将其写入文件

stringObj = StringIO.new

file_id = 'xxxxxx' content = google_drive.export_file(file_id, 'application/pdf', download_dest: stringObj )


File.open('detination.txt', 'w') do |f|
  f.puts(stringObj.read)
end

答案 1 :(得分:0)

就我而言,以上答案无效。我正在使用google-api-client ~> 0.28.4Rails 5.2.2。 我只是想使用Drive API下载Google Spreadsheets。我有一个身份验证服务和一个使用Service Account访问驱动器帐户的服务。

这是将我的Rails服务器中的文件下载到本地文件的服务方法:

def download_file(file_id)
  content = service.export_file(
    file_id,
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    download_dest: "tmp/#{file_id}.xlsx"
  )
end

然后控制器中的文件读取该文件,并将其传递给客户端,如下所示:

def download_file
  file_id = params[:file_id] || ''

  drive_service = GoogleApisService::Drive.new
  file_name = drive_service.download_file(file_id)

  send_file "tmp/#{file_id}.xlsx", type: "application/xlsx", filename: "#{file_id}.xlsx", disposition: 'inline'
end

这就是我将从Google云端硬盘下载的文件保存到临时文件,然后将其提供给响应的方法。