अपने ऐप्लिकेशन में सेव किए गए कॉन्टेंट का शॉर्टकट फ़ाइल बनाएं

Google Drive में तीसरे पक्ष के शॉर्टकट, सिर्फ़ मेटाडेटा की ऐसी फ़ाइलें होती हैं जो बाहरी, तीसरे पक्ष के मालिकाना हक वाले स्टोरेज सिस्टम पर मौजूद दूसरी फ़ाइलों से लिंक होती हैं. ये शॉर्टकट, उन "कॉन्टेंट" फ़ाइलों के रेफ़रंस लिंक के तौर पर काम करते हैं जिन्हें Drive के बाहर का कोई ऐप्लिकेशन सेव करता है. आम तौर पर, ये फ़ाइलें किसी दूसरे डेटास्टोर या क्लाउड स्टोरेज सिस्टम में सेव होती हैं.

तीसरे पक्ष का शॉर्टकट बनाने के लिए, Google Drive API के files.create तरीके का इस्तेमाल करें और MIME टाइप को application/vnd.google-apps.drive-sdk पर सेट करें. फ़ाइल बनाते समय कोई भी कॉन्टेंट अपलोड न करें. ज़्यादा जानकारी के लिए, Google Workspace और Google Drive पर काम करने वाले MIME टाइप देखें.

तीसरे पक्ष के शॉर्टकट अपलोड या डाउनलोड नहीं किए जा सकते.

कोड के नीचे दिए गए सैंपल, क्लाइंट लाइब्रेरी का इस्तेमाल करके तीसरे पक्ष के शॉर्टकट बनाने का तरीका बताते हैं:

Java

drive/snippets/drive_v3/src/main/java/CreateShortcut.java
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
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.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.Arrays;

/* Class to demonstrate Drive's create shortcut use-case */
public class CreateShortcut {

  /**
   * Creates shortcut for file.
   *
   * @throws IOException if service account credentials file not found.
   */
  public static String createShortcut() 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();
    try {
      // Create Shortcut for file.
      File fileMetadata = new File();
      fileMetadata.setName("Project plan");
      fileMetadata.setMimeType("application/vnd.google-apps.drive-sdk");

      File file = service.files().create(fileMetadata)
          .setFields("id")
          .execute();
      System.out.println("File ID: " + file.getId());
      return file.getId();
    } catch (GoogleJsonResponseException e) {
      // TODO(developer) - handle error appropriately
      System.err.println("Unable to create shortcut: " + e.getDetails());
      throw e;
    }
  }
}

Python

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


def create_shortcut():
  """Create a third party shortcut

  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)
    file_metadata = {
        "name": "Project plan",
        "mimeType": "application/vnd.google-apps.drive-sdk",
    }

    # pylint: disable=maybe-no-member
    file = service.files().create(body=file_metadata, fields="id").execute()
    print(f'File ID: {file.get("id")}')

  except HttpError as error:
    print(f"An error occurred: {error}")
  return file.get("id")


if __name__ == "__main__":
  create_shortcut()

PHP

drive/snippets/drive_v3/src/DriveCreateShortcut.php
use Google\Client;
use Google\Service\Drive;
use Google\Service\Drive\DriveFile;
function createShortcut()
{
    try {

        $client = new Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope(Drive::DRIVE);
        $driveService = new Drive($client);
        $fileMetadata = new DriveFile(array(
            'name' => 'Project plan',
            'mimeType' => 'application/vnd.google-apps.drive-sdk'));
        $file = $driveService->files->create($fileMetadata, array(
            'fields' => 'id'));
        printf("File ID: %s\n", $file->id);
        return $file->id;

    } catch(Exception $e) {
        echo "Error Message: ".$e;
    }

}

.NET

drive/snippets/drive_v3/DriveV3Snippets/CreateShortcut.cs
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;

namespace DriveV3Snippets
{
    // Class to demonstrate Drive's create shortcut use-case
    public class CreateShortcut
    {
        /// <summary>
        /// Create a third party shortcut.
        /// </summary>
        /// <returns>newly created shortcut file id, null otherwise.</returns>
        public static string DriveCreateShortcut()
        {
            try
            {
                /* Load pre-authorized user credentials from the environment.
                 TODO(developer) - See https://developers.google.com/identity for 
                 guides on implementing OAuth2 for your application. */
                GoogleCredential credential = GoogleCredential
                    .GetApplicationDefault()
                    .CreateScoped(DriveService.Scope.Drive);

                // Create Drive API service.
                var service = new DriveService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Drive API Snippets"
                });

                // Create Shortcut for file.
                var fileMetadata = new Google.Apis.Drive.v3.Data.File()
                {
                    Name = "Project plan",
                    MimeType = "application/vnd.google-apps.drive-sdk"
                };
                var request = service.Files.Create(fileMetadata);
                request.Fields = "id";
                var file = request.Execute();
                // Prints the shortcut file id.
                Console.WriteLine("File ID: " + file.Id);
                return file.Id;
            }
            catch (Exception e)
            {
                // TODO(developer) - handle error appropriately
                if (e is AggregateException)
                {
                    Console.WriteLine("Credential Not found");
                }
                else
                {
                    throw;
                }
            }
            return null;
        }
    }
}

Node.js

drive/snippets/drive_v3/file_snippets/create_shortcut.js
/**
 * Create a third party shortcut
 * @return{obj} shortcut Id
 * */
async function createShortcut() {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/drive',
  });
  const service = google.drive({version: 'v3', auth});
  const fileMetadata = {
    name: 'Project plan',
    mimeType: 'application/vnd.google-apps.drive-sdk',
  };

  try {
    const file = await service.files.create({
      resource: fileMetadata,
      fields: 'id',
    });
    console.log('File Id:', file.data.id);
    return file.data.id;
  } catch (err) {
    // TODO(developer) - Handle error
    throw err;
  }
}

तीसरे पक्ष के शॉर्टकट कैसे काम करते हैं

जब files.create तरीके का इस्तेमाल करके तीसरे पक्ष का शॉर्टकट बनाया जाता है, तो वह मेटाडेटा डालने और आपके ऐप्लिकेशन के कॉन्टेंट का शॉर्टकट बनाने के लिए, POST के अनुरोध का इस्तेमाल करता है:

POST https://www.googleapis.com/drive/v3/files
Authorization: AUTHORIZATION_HEADER

{
  "title": "FILE_TITLE",
  "mimeType": "application/vnd.google-apps.drive-sdk"
}

जब तीसरे पक्ष के शॉर्टकट पर क्लिक किया जाता है, तो उपयोगकर्ता को उस बाहरी साइट पर रीडायरेक्ट कर दिया जाता है जहां फ़ाइल रखी गई है. Drive का फ़ाइल आईडी, state पैरामीटर में शामिल होता है. ज़्यादा जानकारी के लिए, ऐप्लिकेशन के खास दस्तावेज़ों के लिए ओपन यूआरएल मैनेज करना देखें.

इसके बाद, तीसरे पक्ष के ऐप्लिकेशन या वेबसाइट की यह ज़िम्मेदारी होती है कि वह state पैरामीटर में मौजूद फ़ाइल आईडी को, सिस्टम में मौजूद कॉन्टेंट से मैच करे.

कस्टम थंबनेल और इंडेक्स किया जा सकने वाला टेक्स्ट जोड़ना

तीसरे पक्ष के शॉर्टकट से जुड़ी फ़ाइलों को खोजे जाने की संभावना बढ़ाने के लिए, फ़ाइल का मेटाडेटा डालते समय या उसमें बदलाव करते समय, थंबनेल इमेज और इंडेक्स किया जा सकने वाला टेक्स्ट, दोनों को अपलोड किया जा सकता है. ज़्यादा जानकारी के लिए, फ़ाइल का मेटाडेटा मैनेज करना लेख पढ़ें.