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

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

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

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

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

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

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

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

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

query_term operator values

जगह:

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

  • operator क्वेरी शब्द के लिए शर्त तय करता है.

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

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

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

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

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

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

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

आपको अपनी फ़ाइल के नाम में खास वर्णों को भी शामिल नहीं करना चाहिए, ताकि यह पक्का हो सके कि क्वेरी ठीक से काम कर रही है. उदाहरण के लिए, अगर किसी फ़ाइल नाम में अपॉस्ट्रफ़ी (') और बैकस्लैश ("\") दोनों वर्ण हैं, तो उनसे बचने के लिए बैकस्लैश का इस्तेमाल करें: name contains 'quinn\'s paper\\essay'.

आपको क्या क्वेरी करनी है उदाहरण
"हैलो" नाम वाली फ़ाइलें 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 खोज क्वेरी वाला शब्द इस्तेमाल करें. उदाहरण के लिए, कोई ऐसी कस्टम फ़ाइल प्रॉपर्टी खोजने के लिए जो 8e8aceg2af2ge72e78 वैल्यू के साथ, additionalID नाम के अनुरोध करने वाले ऐप्लिकेशन के लिए निजी है:

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 जैसे अलग-अलग कॉर्पोरा को चुनकर सीमित कर देना चाहिए.