创建云端硬盘文件的快捷方式
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
快捷方式是指链接到 Google 云端硬盘中其他文件或文件夹的文件。
快捷方式具有以下特征:
application/vnd.google-apps.shortcut
MIME 类型。如需了解详情,请参阅 Google Workspace 和 Google 云端硬盘支持的 MIME 类型。
快捷方式的 ACL 会继承父项的 ACL。无法直接更改快捷方式的 ACL。
指向目标文件或文件夹的 targetId
,也称为“目标”。
一个 targetMimeType
,用于指示目标的 MIME 类型。targetMimeType
用于确定要显示的类型图标。创建快捷方式时,目标资源的 MIME 类型会复制到 targetMimeType
字段。
targetId
和 targetMimeType
字段是 文件资源中 shortcutDetails
字段的一部分。
一个快捷方式只能有一个父级。如果其他云端硬盘位置需要快捷方式文件,则可以将该快捷方式文件复制到这些额外的位置。
当目标被删除或当前用户失去对目标的访问权限时,用户指向目标的快捷方式会失效。
快捷方式的标题可以与目标不同。创建快捷方式时,系统会使用目标的标题作为快捷方式的标题。创建后,快捷方式的标题和目标的标题可以单独更改。如果更改了目标的名称,之前创建的快捷方式会保留旧标题。
快捷方式的 MIME 类型可能会过时。虽然很少见,但当上传不同类型的修订版本时,Blob 文件的 MIME 类型会发生变化,不过指向更新后文件的任何快捷方式都会保留原始 MIME 类型。例如,如果您将 JPG 文件上传到云端硬盘,然后上传 AVI 修订版本,云端硬盘会识别出此更改并更新实际文件的缩略图。不过,快捷方式仍会显示 JPG 缩略图。
在 Google 账号数据导出(也称为“Google 导出”)中,快捷方式表示为包含指向目标的链接的 Netscape 书签文件。
如需了解详情,请参阅使用 Google 云端硬盘快捷方式查找文件和文件夹。
创建快捷方式
如需创建快捷方式,请将 MIME 类型设置为 application/vnd.google-apps.shortcut
,将 targetId
设置为快捷方式应链接到的文件或文件夹,然后调用 files.create
以创建快捷方式。
以下示例展示了如何使用客户端库创建快捷方式:
Python
file_metadata = {
'name': 'FILE_NAME',
'mimeType': 'text/plain'
}
file = drive_service.files().create(body=file_metadata, fields='id').execute()
print('File ID: %s' % file.get('id'))
shortcut_metadata = {
'Name': 'SHORTCUT_NAME',
'mimeType': 'application/vnd.google-apps.shortcut',
'shortcutDetails': {
'targetId': file.get('id')
}
}
shortcut = drive_service.files().create(body=shortcut_metadata,
fields='id,shortcutDetails').execute()
print('File ID: %s, Shortcut Target ID: %s, Shortcut Target MIME type: %s' % (
shortcut.get('id'),
shortcut.get('shortcutDetails').get('targetId'),
shortcut.get('shortcutDetails').get('targetMimeType')))
Node.js
var fileMetadata = {
'name': 'FILE_NAME',
'mimeType': 'text/plain'
};
drive.files.create({
'resource': fileMetadata,
'fields': 'id'
}, function (err, file) {
if (err) {
// Handle error
console.error(err);
} else {
console.log('File Id: ' + file.id);
shortcutMetadata = {
'name': 'SHORTCUT_NAME',
'mimeType': 'application/vnd.google-apps.shortcut'
'shortcutDetails': {
'targetId': file.id
}
};
drive.files.create({
'resource': shortcutMetadata,
'fields': 'id,name,mimeType,shortcutDetails'
}, function(err, shortcut) {
if (err) {
// Handle error
console.error(err);
} else {
console.log('Shortcut Id: ' + shortcut.id +
', Name: ' + shortcut.name +
', target Id: ' + shortcut.shortcutDetails.targetId +
', target MIME type: ' + shortcut.shortcutDetails.targetMimeType);
}
}
}
});
替换以下内容:
- FILE_NAME:需要快捷方式的文件名。
- SHORTCUT_NAME:相应快捷方式的名称。
默认情况下,快捷方式会在当前用户的“我的云端硬盘”中创建,并且只会为当前用户有权访问的文件或文件夹创建快捷方式。
搜索快捷方式
如需搜索快捷方式,请使用查询字符串 q
和 files.list
来过滤要返回的快捷方式。
mimeType operator values
其中:
- query_term 是要搜索的查询字词或字段。如需查看可用于过滤共享云端硬盘的搜索查询字词,请参阅搜索查询字词。
- 运算符用于指定查询字词的条件。如需查看可与每个查询字词搭配使用的运算符,请参阅查询运算符。
- 值是指您要用于过滤搜索结果的具体值。
例如,以下查询字符串会过滤搜索结果,以返回所有指向电子表格文件的快捷方式:
q: mimeType='application/vnd.google-apps.shortcut' AND shortcutDetails.targetMimeType='application/vnd.google-apps.spreadsheet'
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Create a shortcut to a Drive file\n\n*Shortcuts* are files that link to other files or folders on Google Drive.\nShortcuts have these characteristics:\n\n- An `application/vnd.google-apps.shortcut` MIME type. For more information,\n see [Google Workspace \\& Google Drive supported MIME\n types](/workspace/drive/api/guides/mime-types).\n\n- The ACL for a shortcut inherits the ACL of the parent. The shortcut's ACL\n cannot be changed directly.\n\n- A `targetId` pointing to the target file or folder, also referred to as the\n \"target.\"\n\n- A `targetMimeType` indicating the MIME type for the target. The\n `targetMimeType` is used to determine the type icon to display. The target's\n MIME type is copied to the `targetMimeType` field when the shortcut is\n created.\n\n- The `targetId` and `targetMimeType` fields are part of the `shortcutDetails`\n field within the [file](/workspace/drive/api/reference/rest/v3/files) resource.\n\n- A shortcut can only have one parent. If a shortcut file is required in other\n Drive locations, the shortcut file can be copied to the\n additional locations.\n\n- When the target is deleted, or when the current user loses access to the\n target, the user's shortcut pointing to the target breaks.\n\n- The title of a shortcut can differ from the target. When a shortcut is\n created, the title of the target is used as the title of the shortcut. After\n creation, the shortcut's title and target's title can be changed\n independently. If the target's name is changed, previously created shortcuts\n retain the old title.\n\n- The MIME type of a shortcut can become stale. While rare, a blob file's MIME\n type changes when a revision of a different type is uploaded, but any\n shortcuts pointing to the updated file retain the original MIME type. For\n example, if you upload a JPG file to Drive, then upload an\n AVI revision, Drive identifies the change and updates the\n thumbnail for the actual file. However, the shortcut continues to have a JPG\n thumbnail.\n\n- In [Google Account Data\n Export](https://support.google.com/accounts/answer/3024190)\n also known as Google Takeout, shortcuts are represented as Netscape\n bookmark files containing links to the target.\n\nFor more information, see [Find files \\& folders with Google Drive\nshortcuts](https://support.google.com/drive/answer/9700156)\n.\n| **Important:** Previously, shortcuts were the term for files that pointed to content stored by an application. That type of \"shortcut\" was renamed to *third-party shortcut* . For further information, see [Create a shortcut file to\n| content stored by your app](/workspace/drive/api/guides/third-party-shortcuts).\n\nCreate a shortcut\n-----------------\n\nTo create a shortcut, set the MIME type to\n`application/vnd.google-apps.shortcut`, set the `targetId` to the file or folder\nthe shortcut should link to, and call [`files.create`](/workspace/drive/api/reference/rest/v3/files/create) to create a shortcut.\n| **Note:** If you're using V2 of the API, use [`files.insert`](/workspace/drive/api/v2/reference/files/insert).\n\nThe following examples show how to create a shortcut using a client library: \n\n### Python\n\n file_metadata = {\n 'name': '\u003cvar translate=\"no\"\u003eFILE_NAME\u003c/var\u003e',\n 'mimeType': 'text/plain'\n }\n file = drive_service.files().create(body=file_metadata, fields='id').execute()\n print('File ID: %s' % file.get('id'))\n shortcut_metadata = {\n 'Name': '\u003cvar translate=\"no\"\u003eSHORTCUT_NAME\u003c/var\u003e',\n 'mimeType': 'application/vnd.google-apps.shortcut',\n 'shortcutDetails': {\n 'targetId': file.get('id')\n }\n }\n shortcut = drive_service.files().create(body=shortcut_metadata,\n fields='id,shortcutDetails').execute()\n print('File ID: %s, Shortcut Target ID: %s, Shortcut Target MIME type: %s' % (\n shortcut.get('id'),\n shortcut.get('shortcutDetails').get('targetId'),\n shortcut.get('shortcutDetails').get('targetMimeType')))\n\n### Node.js\n\n var fileMetadata = {\n 'name': '\u003cvar translate=\"no\"\u003eFILE_NAME\u003c/var\u003e',\n 'mimeType': 'text/plain'\n };\n drive.files.create({\n 'resource': fileMetadata,\n 'fields': 'id'\n }, function (err, file) {\n if (err) {\n // Handle error\n console.error(err);\n } else {\n console.log('File Id: ' + file.id);\n shortcutMetadata = {\n 'name': '\u003cvar translate=\"no\"\u003eSHORTCUT_NAME\u003c/var\u003e',\n 'mimeType': 'application/vnd.google-apps.shortcut'\n 'shortcutDetails': {\n 'targetId': file.id\n }\n };\n drive.files.create({\n 'resource': shortcutMetadata,\n 'fields': 'id,name,mimeType,shortcutDetails'\n }, function(err, shortcut) {\n if (err) {\n // Handle error\n console.error(err);\n } else {\n console.log('Shortcut Id: ' + shortcut.id +\n ', Name: ' + shortcut.name +\n ', target Id: ' + shortcut.shortcutDetails.targetId +\n ', target MIME type: ' + shortcut.shortcutDetails.targetMimeType);\n }\n }\n }\n });\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eFILE_NAME\u003c/var\u003e: the file name requiring a shortcut.\n- \u003cvar translate=\"no\"\u003eSHORTCUT_NAME\u003c/var\u003e: the name for this shortcut.\n\nBy default, the shortcut is created on the current user's My\nDrive and shortcuts are only created for files or folders for\nwhich the current user has access.\n\nSearch for a shortcut\n---------------------\n\nTo search for a shortcut, use the query string `q` with\n[`files.list`](/workspace/drive/api/v3/reference/files/list) to filter the shortcuts to\nreturn.\n\n`mimeType `*operator values*\n\nWhere:\n\n- *query_term* is the query term or field to search upon. To view the query terms that can be used to filter shared drives, refer to [Search query\n terms](/workspace/drive/api/guides/ref-search-terms#file_properties).\n- *operator* specifies the condition for the query term. To view which operators you can use with each query term, refer to [Query operators](/workspace/drive/api/guides/ref-search-terms#operators).\n- *values* are the specific values you want to use to filter your search results.\n\nFor example, the following query string filters the search to return all\nshortcuts to spreadsheet files: \n\n q: mimeType='application/vnd.google-apps.shortcut' AND shortcutDetails.targetMimeType='application/vnd.google-apps.spreadsheet'"]]