返回文件资源中的标签

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

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

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

示例

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

Java

File file = driveService.files().get("FILE_ID").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 方法。

备注