返回文件资源中的标签

本页介绍了如何从 Google 云端硬盘文件资源中返回特定标签。

如需指定要检索的标签,请使用 files.get 方法或返回文件资源的任何方法。请求正文必须为空。

如果成功,响应正文将包含一个 File 实例。

示例

以下代码示例展示了如何使用 fileId 和 labelId 返回一组特定标签。includeLabels 对象是逗号分隔列表的 ID。fields 参数中的 labelInfo 对象包含在 includeLabels 中设置并请求的文件标签。

Java

File file = driveService.files()
    .get("FILE_ID")s
    .setIncludeLabels("LABEL_ID,LABEL_ID")
    .setFields("labelInfo")
    .execute();

Python

file = drive_service.files().get(
    fileId="FILE_ID",
    includeLabels="LABEL_ID,LABEL_ID",
    fields="labelInfo"
).execute()

Node.js

/**
 * Get a Drive file with specific labels
 * @return{obj} file with labelInfo
 **/
async function getFileWithSpecificLabels() {
  // 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});
  try {
    const file = await service.files.get({
      fileId: 'FILE_ID',
      includeLabels: 'LABEL_ID,LABEL_ID',
      fields: 'labelInfo',
    });
    return file;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

替换以下内容:

  • FILE_ID:包含标签的文件的 fileId。
  • LABEL_ID:要返回的标签的 labelId。如需查找文件上的标签,请使用 files.listLabels 方法。

备注