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

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

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

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

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

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

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

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

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

query_term operator values

कहां:

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

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

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

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

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

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 प्रकार की फ़ाइलों तक सीमित नतीजों को सीमित करने के लिए क्वेरी शब्द. इससे यह भी सेट किया जाता है कि खोज को और सटीक बनाने के लिए, Drive तक drive पर spaces स्पेस. जब 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.