簡體   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