फ़ाइलें और फ़ोल्डर खोजना

Google Drive API, फ़ाइलों और फ़ोल्डर को खोजने के लिए कई तरीकों से काम करता है.

Drive में मौजूद किसी उपयोगकर्ता की सभी या कुछ फ़ाइलें और फ़ोल्डर वापस लाने के लिए, files.list तरीके का इस्तेमाल किया जा सकता है. संसाधन के कुछ तरीकों (जैसे, files.get और files.update) के लिए ज़रूरी fileId को वापस पाने के लिए, files.list तरीके का भी इस्तेमाल किया जा सकता है.

मौजूदा उपयोगकर्ता की 'मेरी ड्राइव' पर मौजूद सभी फ़ाइलें और फ़ोल्डर खोजना

सभी फ़ाइलें और फ़ोल्डर दिखाने के लिए, बिना किसी पैरामीटर के files.list तरीके का इस्तेमाल करें.

GET https://www.googleapis.com/drive/v3/files

मौजूदा उपयोगकर्ता की 'मेरी ड्राइव' में मौजूद खास फ़ाइलें या फ़ोल्डर खोजना

फ़ाइलों या फ़ोल्डर के किसी खास सेट को खोजने के लिए, files.list तरीके के साथ क्वेरी स्ट्रिंग q फ़ील्ड का इस्तेमाल करें. इससे, एक या उससे ज़्यादा खोज के लिए शब्दों को मिलाकर, नतीजों के लिए फ़ाइलों को फ़िल्टर किया जा सकता है.

क्वेरी स्ट्रिंग में ये तीन हिस्से होते हैं:

query_term operator values

जगह:

  • query_term, खोजने के लिए क्वेरी शब्द या फ़ील्ड है.

  • operator, क्वेरी के लिए इस्तेमाल किए गए शब्द की शर्त के बारे में बताता है.

  • values वे खास वैल्यू हैं जिनका इस्तेमाल आपको खोज के नतीजों को फ़िल्टर करने के लिए करना है.

फ़िल्टर फ़ाइलों और फ़ोल्डर का इस्तेमाल करके, क्वेरी के लिए इस्तेमाल किए जाने वाले शब्द और ऑपरेटर देखने के लिए, खोज क्वेरी के लिए शब्द और ऑपरेटर देखें.

उदाहरण के लिए, नीचे दी गई क्वेरी स्ट्रिंग, MIME टाइप सेट करके खोज को सिर्फ़ नतीजे दिखाने वाले फ़ोल्डर के लिए फ़िल्टर करती है:

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

MIME टाइप के बारे में ज़्यादा जानकारी के लिए, Google Workspace और Google Drive पर काम करने वाले MIME टाइप देखें.

क्वेरी स्ट्रिंग के उदाहरण

यहां दी गई टेबल में, कुछ बेसिक क्वेरी स्ट्रिंग के उदाहरण दिए गए हैं. असल कोड, खोज के लिए इस्तेमाल की गई क्लाइंट लाइब्रेरी के हिसाब से अलग-अलग होता है.

आप क्या क्वेरी करना चाहते हैं उदाहरण
"हैलो" नाम वाली फ़ाइलें name = 'hello'
"नमस्ते" और "अलविदा" शब्दों के नाम वाली फ़ाइलें name contains 'hello' and name contains 'goodbye'
ऐसे नाम वाली फ़ाइलें जिनमें "हैलो" शब्द नहीं है not name contains 'hello'
ऐसी फ़ाइलें जिनमें "ज़रूरी" टेक्स्ट मौजूद है और जो ट्रैश में हैं fullText contains 'important' and trashed = true
ऐसी फ़ाइलें जिनमें "हैलो" शब्द शामिल हो fullText contains 'hello'
ऐसी फ़ाइलें जिनमें "हैलो" शब्द न हो not fullText contains 'hello'
ऐसी फ़ाइलें जिनमें "नमस्ते दुनिया" वाक्यांश का इस्तेमाल किया गया हो fullText contains '"hello world"'
क्वेरी वाली फ़ाइलें, जिनमें "\" वर्ण (उदाहरण के लिए, "\authors") शामिल है fullText contains '\\authors'
ऐसी फ़ाइलें जो फ़ोल्डर हैं mimeType = 'application/vnd.google-apps.folder'
ऐसी फ़ाइलें जो फ़ोल्डर नहीं हैं mimeType != 'application/vnd.google-apps.folder'
ऐसी फ़ाइलें जिनमें किसी खास तारीख के बाद बदलाव किया गया है (डिफ़ॉल्ट टाइम ज़ोन यूटीसी है) modifiedTime > '2012-06-04T12:00:00'
ऐसी इमेज या वीडियो फ़ाइलें जिनमें किसी खास तारीख के बाद बदलाव किया गया हो modifiedTime > '2012-06-04T12:00:00' and (mimeType contains 'image/' or mimeType contains 'video/')
स्टार के निशान वाली फ़ाइलें starred = true
कलेक्शन में मौजूद फ़ाइलें (उदाहरण के लिए, parents कलेक्शन में मौजूद फ़ोल्डर आईडी) '1234567' in parents
किसी कलेक्शन में मौजूद, ऐप्लिकेशन डेटा फ़ोल्डर में मौजूद फ़ाइलें 'appDataFolder' in parents
ऐसी फ़ाइलें जिनका उपयोगकर्ता "test@example.org" मालिक है 'test@example.org' in owners
ऐसी फ़ाइलें जिनके लिए उपयोगकर्ता "test@example.org" के पास फ़ाइल में बदलाव करने की अनुमति है 'test@example.org' in writers
ऐसी फ़ाइलें जिनके लिए "group@example.org" ग्रुप के सदस्यों के पास फ़ाइल में बदलाव करने की अनुमति है 'group@example.org' in writers
ऐसे उपयोगकर्ता के साथ शेयर की गई फ़ाइलें जिन्हें अनुमति मिली है और जिनके नाम में "हैलो" लिखा हुआ है sharedWithMe and name contains 'hello'
पसंद के मुताबिक बनाई गई फ़ाइल प्रॉपर्टी वाली फ़ाइलें, सभी ऐप्लिकेशन को दिखती हैं properties has { key='mass' and value='1.3kg' }
अनुरोध करने वाले ऐप्लिकेशन के लिए कस्टम फ़ाइल प्रॉपर्टी वाली फ़ाइलें appProperties has { key='additionalID' and value='8e8aceg2af2ge72e78' }
ऐसी फ़ाइलें जिन्हें किसी के साथ या किसी डोमेन के साथ शेयर नहीं किया गया है (सिर्फ़ निजी या कुछ उपयोगकर्ताओं या ग्रुप के साथ शेयर की गई) visibility = 'limited'

क्लाइंट लाइब्रेरी की मदद से खोज के नतीजे फ़िल्टर करना

नीचे दिया गया कोड सैंपल, खोज नतीजों को फ़ाइल के नामों और JPEG फ़ाइलों के आईडी के हिसाब से फ़िल्टर करने के लिए, क्लाइंट लाइब्रेरी का इस्तेमाल करने का तरीका दिखाता है. यह सैंपल, mimeType क्वेरी शब्द का इस्तेमाल करके, image/jpeg टाइप की फ़ाइलों के नतीजों को सटीक बनाता है. यह spaces को drive पर भी सेट कर देता है, ताकि खोज को Drive स्पेस तक और सीमित किया जा सके. जब nextPageToken, null लौटाता है, तो कोई और नतीजा नहीं मिलता.

Java

drive/snippets/drive_v3/src/main/java/SearchFile.java
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* Class to demonstrate use-case of search files. */
public class SearchFile {

  /**
   * Search for specific set of files.
   *
   * @return search result list.
   * @throws IOException if service account credentials file not found.
   */
  public static List<File> searchFile() throws IOException {
           /*Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
           guides on implementing OAuth2 for your application.*/
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(Arrays.asList(DriveScopes.DRIVE_FILE));
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
        credentials);

    // Build a new authorized API client service.
    Drive service = new Drive.Builder(new NetHttpTransport(),
        GsonFactory.getDefaultInstance(),
        requestInitializer)
        .setApplicationName("Drive samples")
        .build();

    List<File> files = new ArrayList<File>();

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

      files.addAll(result.getFiles());

      pageToken = result.getNextPageToken();
    } while (pageToken != null);

    return files;
  }
}

Python

drive/snippets/drive-v3/file_snippet/search_file.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def search_file():
  """Search file in drive location

  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  creds, _ = google.auth.default()

  try:
    # create drive api client
    service = build("drive", "v3", credentials=creds)
    files = []
    page_token = None
    while True:
      # pylint: disable=maybe-no-member
      response = (
          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(f'Found file: {file.get("name")}, {file.get("id")}')
      files.extend(response.get("files", []))
      page_token = response.get("nextPageToken", None)
      if page_token is None:
        break

  except HttpError as error:
    print(f"An error occurred: {error}")
    files = None

  return files


if __name__ == "__main__":
  search_file()

Node.js

drive/snippets/drive_v3/file_snippets/search_file.js
/**
 * Search file in drive location
 * @return{obj} data file
 * */
async function searchFile() {
  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app
  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/drive',
  });
  const service = google.drive({version: 'v3', auth});
  const files = [];
  try {
    const res = await service.files.list({
      q: 'mimeType=\'image/jpeg\'',
      fields: 'nextPageToken, files(id, name)',
      spaces: 'drive',
    });
    Array.prototype.push.apply(files, res.files);
    res.data.files.forEach(function(file) {
      console.log('Found file:', file.name, file.id);
    });
    return res.data.files;
  } catch (err) {
    // TODO(developer) - Handle error
    throw err;
  }
}

PHP

drive/snippets/drive_v3/src/DriveSearchFiles.php
use Google\Client;
use Google\Service\Drive;
function searchFiles()
{
    try {
        $client = new Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope(Drive::DRIVE);
        $driveService = new Drive($client);
        $files = array();
        $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);
            }
            array_push($files, $response->files);

            $pageToken = $response->pageToken;
        } while ($pageToken != null);
        return $files;
    } catch(Exception $e) {
       echo "Error Message: ".$e;
    }
}

कस्टम फ़ाइल प्रॉपर्टी वाली फ़ाइलें खोजना

कस्टम फ़ाइल प्रॉपर्टी वाली फ़ाइलें खोजने के लिए, कुंजी और वैल्यू वाले properties या appProperties खोज क्वेरी शब्द का इस्तेमाल करें. उदाहरण के लिए, कस्टम फ़ाइल प्रॉपर्टी खोजने के लिए जो अनुरोध करने वाले ऐप्लिकेशन के लिए निजी है, additionalID उसकी वैल्यू 8e8aceg2af2ge72e78 है:

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

ज़्यादा जानकारी के लिए, कस्टम फ़ाइल प्रॉपर्टी जोड़ना लेख पढ़ें.

किसी खास लेबल या फ़ील्ड वैल्यू वाली फ़ाइलें खोजें

खास लेबल वाली फ़ाइलें खोजने के लिए, खास लेबल आईडी के साथ labels खोज क्वेरी का इस्तेमाल करें. उदाहरण के लिए: 'labels/LABEL_ID' in labels. कामयाब होने पर, रिस्पॉन्स के मुख्य हिस्से में वे सभी फ़ाइल इंस्टेंस शामिल होते हैं जिनमें लेबल लागू किया गया है.

बिना किसी लेबल आईडी वाली फ़ाइलें खोजने के लिए: Not 'labels/LABEL_ID' in labels.

फ़ील्ड की अलग-अलग वैल्यू के हिसाब से भी फ़ाइलें खोजी जा सकती हैं. उदाहरण के लिए, टेक्स्ट वैल्यू वाली फ़ाइलें खोजने के लिए: labels/LABEL_ID.text_field_id ='TEXT'.

ज़्यादा जानकारी के लिए, किसी खास लेबल या फ़ील्ड वैल्यू वाली फ़ाइलें खोजें देखें.

कॉर्पस खोजें

files.list को कॉल करने वाली खोज, डिफ़ॉल्ट रूप से user के corpora का इस्तेमाल करती हैं. domain के साथ शेयर की गई फ़ाइलें जैसे दूसरे कॉर्पस खोजने के लिए, corpora पैरामीटर सेट करें.

किसी एक क्वेरी में एक से ज़्यादा कॉर्पस की खोज की जा सकती है. हालांकि, अगर मिला-जुला कॉर्पस बहुत बड़ा है, तो अधूरे नतीजे दिखाए जा सकते हैं. अगर जवाब में incompleteSearch की वैल्यू true है, तो इसका मतलब है कि सभी दस्तावेज़ नहीं लौटाए गए हैं. अगर ऐसा होता है, तो आपको user या drive जैसे किसी दूसरे कॉर्पस को चुनकर अपनी क्वेरी को सटीक बनाना चाहिए.