繁体   English   中英

如何使用 ruby 中的 Google Drive api 获取 Google Drive 上文件夹内所有文件的列表

[英]How to get list of all files inside a folder on Google Drive using Google Drive api in ruby

大家好,我在我的应用程序中使用 google-api-client gem 我想获取文件夹中所有图像的列表,我该如何实现它。 这是我的代码,仅用于获取第一页上所有文件的列表。

require 'google/apis/drive_v2'
require 'signet/oauth_2/client'
require 'googleauth'

Drive = Google::Apis::DriveV2 # Alias the module
drive = Drive::DriveService.new
drive.authorization = authorization
files = drive.list_files

请让我知道如何获取文件夹中所有文件的列表。 就像我有一个名为“test_folder”的文件夹,我在该文件夹中有一些文件现在我想获取所有文件的列表。 请帮我。

您需要将参数q与值为'#{folder_id}' an parents

如果文件夹在共享驱动器中,则需要将supports_all_drivesinclude_items_from_all_drives设置为true

下面是我在共享驱动器的指定文件夹中查找具有指定名称的文件的代码。

query = "name = '#{file_name}' and '#{FOLDER_ID}' in parents"

response = drive_service.list_files(
    q: query,
    fields: "files(id, name)",
    supports_all_drives: true,
    include_items_from_all_drives: true
)

希望这有帮助

这里试一下。

service = Google::Apis::DriveV2::DriveService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
# List the 10 most recently modified files.
response = service.list_files()
puts 'Files:'
puts 'No files found' if response.items.empty?
response.items.each do |file|
  puts "#{file.title} (#{file.id})"
end

有关更多信息,请参见文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM