The photos space will sunset in early January 2018. Your users can continue to access Google Photos via the drive space by enabling the Google Photos folder in My Drive in the Drive client settings

Search for Files and Team Drives

You can search for files with the files.list method of the Drive API. Domain administrators may also search for Team Drives in the organization with the teamdrives.admin.list method.

Both methods accept the q parameter which is a search query combining one or more search clauses. Each search clause is made up of three parts.

Field
Attribute of the file that is searched, e.g., the attribute name of the file.
Operator
Test that is performed on the data to provide a match, e.g., contains.
Value
The content of the attribute that is tested, e.g. the name of the file My cool document.

Combine clauses with the conjunctions and or or, and negate the query with not.

Valid fields for files.list

Field Value Type Operators Description
name string contains1, =, != Name of the file.
fullText string contains2 Full text of the file including name, description, content, and indexable text.
mimeType string contains, =, != MIME type of the file.
modifiedTime date3 <=, <, =, !=, >, >= Date of the last modification of the file.
viewedByMeTime date3 <=, <, =, !=, >, >= Date that the user last viewed a file.
trashed boolean =, != Whether the file is in the trash or not.
starred boolean =, != Whether the file is starred or not.
parents collection in Whether the parents collection contains the specified ID.
owners4 collection in Users who own the file.
writers4 collection in Users or groups who have permission to modify the file.
readers4 collection in Users or groups who have permission to read the file.
sharedWithMe boolean =, != Files that are in the user's "Shared with me" collection.
properties collection has Public custom file properties.
appProperties collection has Private custom file properties.
visibility string =, '!=' The visibility level of the file. Valid values are anyoneCanFind, anyoneWithLink, domainCanFind, domainWithLink, and limited.

Valid fields for teamdrives.list

Field Value Type Operators Description
name5 string contains, =, != Name of the Team Drive.
createdTime5 date <=, <, =, !=, >, >= Date when the Team Drive was created.
memberCount5 number <=, <, =, !=, >, >= Number of users and groups that are members of the Team Drive.
organizerCount5 number <=, <, =, !=, >, >= Number of users and groups that are organizers of the Team Drive.

Value types

Value Type Notes
String Surround with single quotes '. Escape single quotes in queries with \', e.g., 'Valentine\'s Day'.
Boolean true or false.
Date RFC 3339 format, default timezone is UTC, e.g., 2012-06-04T12:00:00-08:00.
Number A numerical value.

Operators

Operator Notes
contains The content of one string is present in the other.
= The content of a string or boolean is equal to the other.
!= The content of a string or boolean is not equal to the other.
< A value is less than another
<= A value is less than or equal to another.
> A value is later than another.
>= A value is later than or equal to another.
in An element is contained within a collection.
and Return items that match both clauses.
or Return items that match either clause.
not Negates a search clause.
has A collection contains an element matching the parameters.

For compound clauses, you can use parentheses to group clauses together. For example:

modifiedTime > '2012-06-04T12:00:00' and (mimeType contains 'image/' or mimeType contains 'video/')

This search returns all files with an image or video MIME type that were last modified after June 4, 2012. Because and and or operators are evaluated from left to right, without parentheses the above example would return only images modified after June 4, 2012, but would return all videos, even those before June 4, 2012.

Examples

All examples on this page show the unencoded q parameter, where name = 'hello' is encoded as name+%3d+%27hello%27. Client libraries handle this encoding automatically.

Examples for files.list

Search for files with the name "hello"

name = 'hello'

Search for folders using the folder-specific MIME type

mimeType = 'application/vnd.google-apps.folder'

Search for files that are not folders

mimeType != 'application/vnd.google-apps.folder'

Search for files with a name containing the words "hello" and "goodbye"

name contains 'hello' and name contains 'goodbye'

Search for files with a name that does not contain the word "hello"

not name contains 'hello'

Search for files containing the word "hello" in the content

fullText contains 'hello'

Search for files not containing the word "hello" in the content

not fullText contains 'hello'

Search for files containing the exact phrase "hello world" in the content

fullText contains '"hello world"'
fullText contains '"hello_world"'

Search for files with a query containing the "\" character (e.g., "\authors")

fullText contains '\\authors'

Search for files writeable by the user "test@example.org"

'test@example.org' in writers

Search for files writeable by the members of the group "group@example.org"

'group@example.org' in writers

Search for the ID 1234567 in the parents collection. This finds all files and folders located directly in the folder whose ID is 1234567.

'1234567' in parents

Search for the alias ID appDataFolder in the parents collection. This finds all files and folders located directly under the Application Data folder.

'appDataFolder' in parents

Search for files writeable by the users "test@example.org" and "test2@example.org"

'test@example.org' in writers and 'test2@example.org' in writers

Search for files containing the text "important" which are in the trash

fullText contains 'important' and trashed = true

Search for files modified after June 4th 2012

modifiedTime > '2012-06-04T12:00:00'    // default time zone is UTC
modifiedTime > '2012-06-04T12:00:00-08:00'

Search for files shared with the authorized user with "hello" in the name

sharedWithMe and name contains 'hello'

Search for files with a custom file property named additionalID with the value 8e8aceg2af2ge72e78.

appProperties has { key='additionalID' and value='8e8aceg2af2ge72e78' }

Search for files that have not been shared with anyone or domains (only private, or shared with specific users or groups)

visibility = 'limited'

Examples for teamdrives.list

Search for Team Drives with no assigned organizer

organizerCount = 0

Search for Team Drives created after June 1, 2017

createdTime > '2017-06-01T12:00:00'

Search for Team Drives with the word 'confidential' in the title and 20 or more members

name contains 'confidential' and memberCount >= 20

Using corpora

Searches are also affected by the corpora selected when calling files.list. If unspecified, the user corpus is used. To search other corpora, such as files shared to a G Suite domain or Team Drives, use the corpora parameter.

Multiple corpora and Team Drives 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.

Using client libraries

The following examples shows how to perform a search of image files:

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;

PHP

$pageToken = null;
do {
    $response = $driveService->files->listFiles(array(
        'q' => "mimeType='image/jpeg'",
        'spaces' => 'drive',
        'pageToken' => $pageToken,
        'fields' => 'nextPageToken, files(id, name)',
    ));
    foreach ($response->files as $file) {
        printf("Found file: %s (%s)\n", $file->name, $file->id);
    }

    $pageToken = $repsonse->pageToken;
} while ($pageToken != null);

.NET

string pageToken = null;
do
{
    var request = driveService.Files.List();
    request.Q = "mimeType='image/jpeg'";
    request.Spaces = "drive";
    request.Fields = "nextPageToken, files(id, name)";
    request.PageToken = pageToken;
    var result = request.Execute();
    foreach (var file in result.Files)
    {
        Console.WriteLine(String.Format(
                "Found file: {0} ({1})", file.Name, file.Id));
    }
    pageToken = result.NextPageToken;
} while (pageToken != null);

Ruby

files = drive_service.fetch_all(items: :files) do |page_token|
  drive_service.list_files(q: "mimeType='image/jpeg'",
                           spaces: 'drive',
                           fields: 'nextPageToken, files(id, name)',
                           page_token: page_token)
end
for file in files
  # Process change
  puts "Found file: #{file.name} #{file.id}"
end

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
  }
})

Objective-C

GTLRDriveQuery_FilesList *query = [GTLRDriveQuery_FilesList query];
query.q = @"mimeType='image/jpeg'";
query.spaces = @"drive";
query.fields = @"nextPageToken, files(id, name)";
[driveService executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
                                                     GTLRDrive_FileList *files,
                                                     NSError *error) {
    if (error == nil) {
        for(GTLRDrive_File *file in files) {
            NSLog(@"Found file: %@ (%@)", file.name, file.identifier);
        }
    } else {
        NSLog(@"An error occurred: %@", error);
    }
}];

发送以下问题的反馈:

此网页
Drive REST API
Drive REST API