Create a shortcut file to content stored by your app

Third-party shortcuts in Google Drive are metadata-only files that link to other files on external, third-party owned, storage systems. These shortcuts act as reference links to the "content" files stored by an application outside of Drive, usually in a different datastore or cloud storage system.

To create a third-party shortcut, use the files.create method of the Google Drive API and set the MIME type to application/vnd.google-apps.drive-sdk. Don't upload any content when creating the file. For more information, see Google Workspace and Google Drive supported MIME types.

You cannot upload or download third-party shortcuts.

The following code samples show how to create a third-party shortcut using a client library:

Java

drive/snippets/drive_v3/src/main/java/CreateShortcut.java
importcom.google.api.client.googleapis.json.GoogleJsonResponseException;
importcom.google.api.client.http.HttpRequestInitializer;
importcom.google.api.client.http.javanet.NetHttpTransport;
importcom.google.api.client.json.gson.GsonFactory;
importcom.google.api.services.drive.Drive;
importcom.google.api.services.drive.DriveScopes;
importcom.google.api.services.drive.model.File;
importcom.google.auth.http.HttpCredentialsAdapter;
importcom.google.auth.oauth2.GoogleCredentials;
importjava.io.IOException;
importjava.util.Arrays;
/* Class to demonstrate Drive's create shortcut use-case */
publicclass CreateShortcut{
/**
 * Creates shortcut for file.
 *
 * @throws IOException if service account credentials file not found.
 */
publicstaticStringcreateShortcut()throwsIOException{
/* Load pre-authorized user credentials from the environment.
 TODO(developer) - See https://developers.google.com/identity for
 guides on implementing OAuth2 for your application.*/
GoogleCredentialscredentials=GoogleCredentials.getApplicationDefault()
.createScoped(Arrays.asList(DriveScopes.DRIVE_FILE));
HttpRequestInitializerrequestInitializer=newHttpCredentialsAdapter(
credentials);
// Build a new authorized API client service.
Driveservice=newDrive.Builder(newNetHttpTransport(),
GsonFactory.getDefaultInstance(),
requestInitializer)
.setApplicationName("Drive samples")
.build();
try{
// Create Shortcut for file.
FilefileMetadata=newFile();
fileMetadata.setName("Project plan");
fileMetadata.setMimeType("application/vnd.google-apps.drive-sdk");
Filefile=service.files().create(fileMetadata)
.setFields("id")
.execute();
System.out.println("File ID: "+file.getId());
returnfile.getId();
}catch(GoogleJsonResponseExceptione){
// TODO(developer) - handle error appropriately
System.err.println("Unable to create shortcut: "+e.getDetails());
throwe;
}
}
}

Python

drive/snippets/drive-v3/file_snippet/create_shortcut.py
importgoogle.auth
fromgoogleapiclient.discoveryimport build
fromgoogleapiclient.errorsimport HttpError
defcreate_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
<?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
usingGoogle.Apis.Auth.OAuth2;
usingGoogle.Apis.Drive.v3;
usingGoogle.Apis.Services;
namespaceDriveV3Snippets
{
// Class to demonstrate Drive's create shortcut use-case
publicclassCreateShortcut
{
/// <summary>
/// Create a third party shortcut.
/// </summary>
/// <returns>newly created shortcut file id, null otherwise.</returns>
publicstaticstringDriveCreateShortcut()
{
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. */
GoogleCredentialcredential=GoogleCredential
.GetApplicationDefault()
.CreateScoped(DriveService.Scope.Drive);
// Create Drive API service.
varservice=newDriveService(newBaseClientService.Initializer
{
HttpClientInitializer=credential,
ApplicationName="Drive API Snippets"
});
// Create Shortcut for file.
varfileMetadata=newGoogle.Apis.Drive.v3.Data.File()
{
Name="Project plan",
MimeType="application/vnd.google-apps.drive-sdk"
};
varrequest=service.Files.Create(fileMetadata);
request.Fields="id";
varfile=request.Execute();
// Prints the shortcut file id.
Console.WriteLine("File ID: "+file.Id);
returnfile.Id;
}
catch(Exceptione)
{
// TODO(developer) - handle error appropriately
if(eisAggregateException)
{
Console.WriteLine("Credential Not found");
}
else
{
throw;
}
}
returnnull;
}
}
}

Node.js

drive/snippets/drive_v3/file_snippets/create_shortcut.js
import{GoogleAuth}from'google-auth-library';
import{google}from'googleapis';
/**
 * Creates a shortcut to a third-party resource.
 * @return {Promise<string|null|undefined>} The shortcut ID.
 */
asyncfunctioncreateShortcut(){
// Authenticate with Google and get an authorized client.
// TODO (developer): Use an appropriate auth mechanism for your app.
constauth=newGoogleAuth({
scopes:'https://www.googleapis.com/auth/drive',
});
// Create a new Drive API client (v3).
constservice=google.drive({version:'v3',auth});
// The metadata for the new shortcut.
constfileMetadata={
name:'Project plan',
mimeType:'application/vnd.google-apps.drive-sdk',
};
// Create the new shortcut.
constfile=awaitservice.files.create({
requestBody:fileMetadata,
fields:'id',
});
// Print the ID of the new shortcut.
console.log('File Id:',file.data.id);
returnfile.data.id;
}

How third-party shortcuts work

When you create a third-party shortcut using the files.create method, it uses a POST request to insert the metadata and create a shortcut to your app's content:

POST https://www.googleapis.com/drive/v3/files
Authorization: AUTHORIZATION_HEADER
{
 "title": "FILE_TITLE",
 "mimeType": "application/vnd.google-apps.drive-sdk"
}

When the third-party shortcut is clicked, the user is redirected to the external site where the file is housed. The Drive file ID is contained in the state parameter. For more information, see Handle an Open URL for app-specific documents.

The third-party app or website is then responsible for matching the file ID in the state parameter to the content housed within their system.

Add custom thumbnails and indexable text

To increase the discoverability of files associated with third-party shortcuts, you can upload both thumbnail images and indexable text when inserting or modifying the file metadata. For more information, see Manage file metadata.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月13日 UTC.