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

问题描述 投票:2回答:2

我正在尝试使用Ruby下载google文档文件。我找不到下载文件的路径。我用ruby中的'export_file'方法命令如下:

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

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

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

=> #

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

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

最好

ruby-on-rails ruby google-api-client
2个回答
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

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云端硬盘保存到临时文件的方式,然后将其提供给响应。

© www.soinside.com 2019 - 2024. All rights reserved.