创建云端硬盘文件的快捷方式

快捷方式是链接到 Google 云端硬盘中其他文件或文件夹的文件。 快捷方式具有以下特征:

  • application/vnd.google-apps.shortcut MIME 类型。如需了解详情,请参阅 Google Workspace 和 Google 云端硬盘支持的 MIME 类型

  • 快捷方式的 ACL 继承父项的 ACL。无法直接更改快捷方式的 ACL。

  • 指向目标文件或文件夹的 targetId,也称为“目标”。

  • targetMimeType,指示目标的 MIME 类型。targetMimeType 用于确定要显示的类型图标。创建快捷方式时,目标的 MIME 类型会复制到 targetMimeType 字段。

  • targetIdtargetMimeType 字段是 file 资源中的 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:此快捷方式的名称。

默认情况下,系统会在当前用户的“我的云端硬盘”中创建快捷方式,并且仅针对当前用户有权访问的文件或文件夹创建快捷方式。

搜索快捷方式

如需搜索快捷方式,请将查询字符串 qfiles.list 结合使用,以过滤要返回的快捷方式。

mimeType operator values

其中:

  • query_term 是要搜索的查询字词或字段。如需查看可用于过滤共享云端硬盘的查询字词,请参阅搜索查询字词
  • operator 用于指定查询字词的条件。如需了解您可以用于每个查询字词的运算符,请参阅查询运算符
  • values 是要用于过滤搜索结果的具体值。

例如,以下查询字符串会对搜索进行过滤,以返回电子表格文件的所有快捷方式:

q: mimeType='application/vnd.google-apps.shortcut' AND shortcutDetails.targetMimeType='application/vnd.google-apps.spreadsheet'