Search for files and folders

You can search for files with the Drive API files: list method. You can call Files.list without any parameters, which returns all files on the user's drive. By default, Files.list only returns a subset of properties for a resource. If you want more properties returned, use the fields parameter that specifies which properties to return in the query string q. To make your search query more specific, you can use several operators with each query property.

A basic query contains three parts: a property, an operator and a value for the returned query result. This example shows a basic query:

 `name` `=` 'My document'

Where:

  • The resource property is name.
  • The operator specifies the rule = for the search.
  • The value for the name of the file is "My document".

You can modify your searches using the query parameter operators. To see which operators you can use with each query field, see: File search query reference.

Search for image files using client libraries

These examples show how to search for JPEG image files and return the list of file ids and titles.

You need to specify the file type, the search location and provide the file metadata you want returned in the list of files (e.g. file id and title).

File type field mimeType is set to value 'image/jpeg' to return only JPEG files.

The location field spaces is set to search within drive. Other locations include appDataFolder or photos.

To conduct a search of all pages, you need the nextPageToken to search each page's token, until no pages remain and the search ends when it returns null. You also provide the file metadata fields you want returned in the file list. For items[] list, pass in id and title to return the file id and title in the file list.

For more details about the Drive API fields and return values, see files: list.

Java

String pageToken = null;
do {
  FileList result = driveService.files().list()
      .setQ("mimeType='image/jpeg'")
      .setSpaces("drive")
      .setFields("nextPageToken, files(id, name)")
      .setPageToken(pageToken)
      .execute();
  for (File file : result.getFiles()) {
    System.out.printf("Found file: %s (%s)\n",
        file.getName(), file.getId());
  }
  pageToken = result.getNextPageToken();
} while (pageToken != null);

Python

page_token = None
while True:
    response = drive_service.files().list(q="mimeType='image/jpeg'",
                                          spaces='drive',
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()
    for file in response.get('files', []):
        # Process change
        print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break

Node.js

var pageToken = null;
// Using the NPM module 'async'
async.doWhilst(function (callback) {
  drive.files.list({
    q: "mimeType='image/jpeg'",
    fields: 'nextPageToken, files(id, name)',
    spaces: 'drive',
    pageToken: pageToken
  }, function (err, res) {
    if (err) {
      // Handle error
      console.error(err);
      callback(err)
    } else {
      res.files.forEach(function (file) {
        console.log('Found file: ', file.name, file.id);
      });
      pageToken = res.nextPageToken;
      callback();
    }
  });
}, function () {
  return !!pageToken;
}, function (err) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    // All pages fetched
  }
})

Query search examples

This table shows some basic search query examples. The actual code will be different depending on the client library you use for your search.

What you want to query Example
Files with the name "hello" name = 'hello'
Files with a name containing the words "hello" and "goodbye" name contains 'hello' and name contains 'goodbye'
Files with a name that does not contain the word "hello" not name contains 'hello'
Folders that are Google apps or have the folder MIME type mimeType = 'application/vnd.google-apps.folder'
Files that are not folders mimeType != 'application/vnd.google-apps.folder'
Files that contain the text "important" and in the trash fullText contains 'important' and trashed = true
Files that contain the word "hello" fullText contains 'hello'
Files that do not have the word "hello" not fullText contains 'hello'
Files that contain the exact phrase "hello world" fullText contains '"hello world"'
Files with a query that contains the "" character (e.g., "\authors") fullText contains '\\authors'
Files with ID within a collection, e.g. parents collection '1234567' in parents
Files in an Application data folder in a collection 'appDataFolder' in parents
Files with that users have write permission e.g. user "test@example.org" 'test@example.org' in writers
Files that user "test@example.org" has write permission 'test@example.org' in writers
Files that members of the group "group@example.org" have write permission 'group@example.org' in writers
Fles by date modified modifiedTime > '2012-06-04T12:00:00' // default time zone is UTC
Files shared with the authorized user with "hello" in the name sharedWithMe and name contains 'hello'
Files that have not been shared with anyone or domains (only private, or shared with specific users or groups) visibility = 'limited'
Files modified by a specific date that are either images or video modifiedTime > '2012-06-04T12:00:00' and (mimeType contains 'image/' or mimeType contains 'video/')

Search the corpora

Searches that call files: list use user corpus by default. To search other corpora, such as files shared to a G Suite domain, use the corpora parameter.

Multiple corpora may be searched in a single query, though incomplete results may be returned if the combined corpus is too large. If the incompleteSearch field in the result is true, not all documents have been searched.

다음에 대한 의견 보내기...

도움이 필요하시나요? 지원 페이지를 방문하세요.