如何使用gem' google_drive'获取文件类型和创建日期/时间?在Rails 4?

时间:2016-06-23 05:21:15

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

我正在开发一个Rails应用程序,该应用程序使用以下宝石获取Google云端硬盘中特定文件夹下的文件:

gem 'google_drive'
gem 'google-api-client', '0.9'
gem 'googleauth'

从此存储库github

我的问题是返回的结果包含

* filename
* file id

我的代码如下:

result = []

response = @service.list_files(
  q: "mimeType = 'application/vnd.google-apps.folder' and 'xxxx' in parents",
  order_by: 'createdTime desc',
  spaces: 'drive',
  fields:'nextPageToken, files(id, name)'
)

response.files.each do |file|
  result << file
end

我希望文件的类型和日期时间创建或上传

我需要创建/上传的dateTime用于显示目的

虽然mime类型适合我获取Supported Mime Type,因为我将用于查询API。

另外,我想确定这是否是从浏览器(客户端)下载文件的简单和最佳方式,因为我已经获得 FILEID

<a href="https://www.googleapis.com/drive/v3/files/<%= file_id] %>?alt=media">

请帮忙!

1 个答案:

答案 0 :(得分:0)

<强>解决

获取 uploadedDateTime &amp;每个文件的 mimeType

我在字段中添加了其他选项:

response = @service.list_files(
      q: "'yyyyy' in parents",
      order_by: 'createdTime desc, modifiedTime desc, name',
      spaces: 'drive',
      fields:'nextPageToken, files(id, name, createdTime, mimeType)',
      page_size: 20
    )

感谢@ d.datul1990

的建议

但是,为此:

The Children and Parents collections have been removed. Use files.list instead.

我不需要更新当前代码,因为我使用了 ruby​​ gems (gem&#39; google_drive&#39;),它们有自己的功能。

要在浏览器中下载文件:注意:文件不适用于公开,只有登录用户才能下载公司电子邮件。)

由于我已经拥有 file_id ,我确实喜欢:

<a href="https://drive.google.com/a/xxx.net/file/d/file_id_here/view?usp=sharing" target="_blank">Click to Open/Download</a>
  

希望它可以帮助他人。