移除文件的标签

本页面介绍如何移除单个 Google 云端硬盘文件的标签。

如需从文件中移除文件标签元数据,请使用 files.modifyLabels 方法。请求正文包含一个 ModifyLabelsRequest 实例,用于修改文件上的标签集。请求可能包含多项以原子方式应用的修改。也就是说,如果任何修改无效,则整个更新都将失败,并且不会应用任何(可能相关的)更改。

ModifyLabelsRequest 包含 LabelModification 的一个实例,该实例是对文件标签的修改。它还可能包含 FieldModification 的实例,该实例是对标签字段的修改。如需从文件中移除标签,请将 FieldModification.removeLabel 设置为 True

如果成功,响应正文将包含请求添加或更新的标签。这些按钮位于类型为 LabelmodifiedLabels 对象中。

示例

以下代码示例展示了如何使用 labelId 通过 fileId 移除与标签关联的所有字段。例如,如果某个标签同时包含文本字段和用户字段,则移除标签会同时删除与该标签关联的文本字段和用户字段。然而,取消设置文本字段会将其从标签中移除,但用户字段保持不变。如需了解详情,请参阅取消设置文件的标签字段

Java

ModifyLabelsRequest modifyLabelsRequest =
  new ModifyLabelsRequest()
      .setLabelModifications(
          ImmutableList.of(
              new LabelModification()
                .setLabelId("LABEL_ID")
                .setRemoveLabel(true)));

ModifyLabelsResponse modifyLabelsResponse = driveService.files().modifyLabels("FILE_ID", modifyLabelsRequest).execute();

Python

label_modification = {'labelId':'LABEL_ID', 'removeLabel': True]}

modified_labels = drive_service.files().modifyLabels(fileId="FILE_ID", body = {'labelModifications' : [label_modification]}).execute();

Node.js

/**
* Remove a label on a Drive file
* @return{obj} updated label data
**/
async function removeLabel() {
  // 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});
  const labelModification = {
    'labelId': 'LABEL_ID',
    'removeLabel': True,
  };
  const labelModificationRequest = {
    'labelModifications': [labelModification],
  };
  try {
    const updateResponse = await service.files.modifyLabels({
      fileId: 'FILE_ID',
      resource: labelModificationRequest,
    });
    return updateResponse;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }

替换以下内容:

  • LABEL_ID:要修改的标签的 labelId。如需在文件中查找标签,请使用 files.listLabels 方法。
  • FILE_ID:修改了标签的文件的 fileId