main-branch/drive_v3

View on GitHub
examples/file_search

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true

# Search for files owned by the current user
#
# Returns a [FileList](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-drive_v3/lib/google/apis/drive_v3/classes.rb)
# object. The FileList object has a `files` property that is an array of files returned.
#
# This example illustrates how to:
#   1. specify a search query (to omit files in the trash)
#   2. specify which fields to return for each file
#   3. retrieve pagenated results
#
# Use the `jq` command to format the output of this script. For example:
#   * List names: `bundle exec examples/list_files | jq '.[].name'`
#   * List names and id's: `bundle exec examples/list_files | jq '[.[] | {name: .name, id: .id}]'`
#   * List names and parents: `bundle exec examples/list_files | jq '[.[] | {name: .name, parents: .parents}]'`

require 'drive_v3'

@drive_service = DriveV3.drive_service

@parents = {}

def parent(id)
  @parents[id] ||= @drive_service.get_file(id, fields: 'name, web_view_link')
end

files = []

# Number of files to return with each request
#
# Limiting to 5 for this example, normally this would be a lot higher
# (default is 100)
#
page_size = 100

# Signal to get first page of results
#
# nextPageToken property returns the page_token of the next page
# of results. If nextPageToken is empty, there are no more pages.
#
page_token = nil

# By default `list_files` returns all files even files in the trash. To omit trashed
# files add `trashed=false` to the q (query) parameter.
#
# See [Search for files & folders](https://developers.google.com/drive/api/v3/search-files)
# for more information about how to use the `q` parameter.
#
# q = "trashed=false and '1V6RS_7_YgJmLDH-BDw3dpD8Np60Oi9ET' in parents"
q = 'trashed=false'

# If `fields` is not specified, the following fields are returned for each file:
# id, name, mimeType, and kind.
#
# Could return all possible fields by specifying `fields: '*'`. In this example,
# we only need `nextPageToken` for the request and `id`, `name`, and `parents`,
# and `web_view_link` for each file.
#
# If you give fields, you must explicitly include `nextPageToken` if you want it.
#
# See [Return specific fields for a file](https://developers.google.com/drive/api/guides/fields-parameter)
# for more information about how to retrieve other fields.
#
# fields = 'nextPageToken, files(id, name, parents, web_view_link)'
fields = '*'

# If `include_items_from_all_drives` is false or omitted, only files from the user's
# default drive are returned.
#
include_items_from_all_drives = true

# Indicate that this application supports files in shared drives. An error will result if
# this is false (or omitted) AND `list_files` returns a file from a shared drive.
#
supports_all_drives = true

loop do
  file_list = @drive_service.list_files(
    q:,
    fields:,
    include_items_from_all_drives:,
    supports_all_drives:,
    page_token:, page_size:
  )
  files += file_list.files.map(&:to_h)
  # Stop looping if there are no more pages
  break unless (page_token = file_list.next_page_token)
end

# Print as JSON with links to each file and its parents

puts JSON.pretty_generate(files)
# puts files.count