保护文件内容

Google Drive API 支持多种阻止文件修改的方法,包括限制文件内容以及禁止下载、打印或复制文件。

通过云端硬盘内容限制将文件设为只读

您可以向 Google 云端硬盘文件添加内容限制,以防止用户执行以下操作:

  • 修改标题
  • 修改内容
  • 上传修订版本
  • 添加或修改评论

应用内容限制是一种机制,可在不更改云端硬盘内容的访问权限权限的情况下,将云端硬盘内容的内容设为只读。这意味着这不是访问权限限制。虽然用户无法修改文件的内容,但您仍然可以根据访问权限级别执行其他操作(例如,具有编辑权限的用户仍然可以移动项或更改其共享设置)。

如需为云端硬盘中的文件添加或移除内容限制,用户必须具有关联的权限。对于“我的云端硬盘”中具有 capabilities.canModifyEditorContentRestriction 的文件或文件夹,您必须先分配 role=writer。对于“我的云端硬盘”中具有 ownerRestricted 内容限制的文件或文件夹,您必须拥有相应文件的所有权或具有 role=organizer 权限。如需查看具有内容限制的项,用户必须具备 role=reader 或更高版本。如需查看完整的角色列表,请参阅角色和权限。如需更改文件的权限,请参阅更改权限

您可以使用 files 资源中的 contentRestrictions.readOnly 布尔值字段设置内容限制。请注意,为项设置内容限制会覆盖现有项。

内容限制的场景

针对云端硬盘内容的内容限制可向用户表明不应更改内容。原因可能如下:

  • 在审核或审核期间暂停协作文档处理。
  • 将商品设置为最终确定状态,例如已批准。
  • 防止在敏感会议期间进行更改。
  • 禁止对自动化系统处理的工作流进行外部更改。
  • 限制通过 Google Apps 脚本和 Google Workspace 插件进行修改。
  • 避免意外编辑文档。

请注意,虽然内容限制有助于管理内容,但这并不意味着具有足够权限的用户可以继续处理某个内容。此外,它也不是创建不可变记录的方法。云端硬盘内容限制是可变的,因此针对项设置的内容限制不能保证该项永远不会更改。

管理具有内容限制的文件

Google 文档、Google 表格、Google 幻灯片以及所有其他文件可以包含内容限制。

针对项的内容限制可防止更改其标题和内容,包括:

  • 评论和建议(针对 Google 文档、表格、幻灯片和二进制文件)
  • 二进制文件的修订版本
  • Google 文档中的文本和格式设置
  • Google 表格中的文本或公式、表格布局 以及 Google 表格中的实例
  • Google 幻灯片中的所有内容,以及幻灯片的顺序和数量

某些文件类型不能包含内容限制。下面是几个例子:

添加内容限制

如需添加文件内容限制,请使用 files.update 方法,并将 contentRestrictions.readOnly 字段设置为 true。添加可选的 reason 来说明添加限制的原因,例如“已敲定的合同”。以下代码示例展示了如何添加内容限制:

Java

File updatedFile =
  new File()
      .setContentRestrictions(
          ImmutableList.of(new ContentRestriction().setReadOnly(true).setReason("Finalized contract."));

File response = driveService.files().update("FILE_ID", updatedFile).setFields("contentRestrictions").execute();

Python

content_restriction = {'readOnly': True, 'reason':'Finalized contract.'}

response = drive_service.files().update(fileId="FILE_ID", body = {'contentRestrictions' : [content_restriction]}, fields = "contentRestrictions").execute();

Node.js

/**
* Set a content restriction on a file.
* @return{obj} updated file
**/
async function addContentRestriction() {
  // 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 contentRestriction = {
    'readOnly': True,
    'reason': 'Finalized contract.',
  };
  const updatedFile = {
    'contentRestrictions': [contentRestriction],
  };
  try {
    const response = await service.files.update({
      fileId: 'FILE_ID',
      resource: updatedFile,
      fields: 'contentRestrictions',
    });
    return response;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

FILE_ID 替换为您要修改的文件的 fileId

运行示例代码时,文件受内容限制,并且 Google 云端硬盘界面 (UI) 中的文件名旁边会显示一个锁形符号 ()。该文件现在处于只读状态。

云端硬盘文件列表中具有内容限制的文件。
图 1. 云端硬盘文件列表中具有内容限制的文件。

移除内容限制

如需移除文件内容限制,请使用 files.update 方法,并将 contentRestrictions.readOnly 字段设置为 false。以下代码示例展示了如何移除内容限制:

Java

File updatedFile =
new File()
    .setContentRestrictions(
        ImmutableList.of(new ContentRestriction().setReadOnly(false));

File response = driveService.files().update("FILE_ID", updatedFile).setFields("contentRestrictions").execute();

Python

content_restriction = {'readOnly': False}

response = drive_service.files().update(fileId="FILE_ID", body = {'contentRestrictions' : [content_restriction]}, fields = "contentRestrictions").execute();

Node.js

/**
* Remove a content restriction on a file.
* @return{obj} updated file
**/
async function removeContentRestriction() {
  // 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 contentRestriction = {
    'readOnly': False,
  };
  const updatedFile = {
    'contentRestrictions': [contentRestriction],
  };
  try {
    const response = await service.files.update({
      fileId: 'FILE_ID',
      resource: updatedFile,
      fields: 'contentRestrictions',
    });
    return response;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

FILE_ID 替换为您要修改的文件的 fileId

当您运行示例代码时,文件将不再受内容限制。

您还可以使用云端硬盘界面移除内容限制并允许内容修改(前提是您拥有正确的权限)。您可以通过以下两种方式执行此操作:

  1. 在云端硬盘中,右键点击包含内容限制的文件,然后点击解锁

    移除云端硬盘文件列表中的文件内容限制。
    图 2. 移除云端硬盘文件列表中的文件内容限制。
  2. 打开有内容限制的文件,然后点击 (锁定模式) > 解锁文件

    移除文档中的文件内容限制。
    图 3. 移除文档中的文件内容限制。

检查内容限制

如需检查是否存在内容限制,请使用带有 contentRestrictions 返回的字段的 files.get 方法。以下代码示例展示了如何检查内容限制的状态:

Java

File response = driveService.files().get("FILE_ID").setFields("contentRestrictions").execute();

Python

response = drive_service.files().get(fileId="FILE_ID", fields = "contentRestrictions").execute();

Node.js

/**
* Get content restrictions on a file.
* @return{obj} updated file
**/
async function fetchContentRestrictions() {
  // 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 response = await service.files.get({
      fileId: 'FILE_ID',
      fields: 'contentRestrictions',
    });
    return response;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

FILE_ID 替换为您要检查的文件的 fileId

运行示例代码时,该方法会返回 ContentRestriction 资源(如果存在)。

添加只有文件所有者可以修改的内容限制

如需添加文件内容限制,以便只有文件所有者才能切换该机制,请使用 files.update 方法,并将 contentRestrictions.ownerRestricted 布尔值字段设置为 true。以下代码示例展示了如何仅针对文件所有者添加内容限制:

Java

File updatedFile =
  new File()
      .setContentRestrictions(
          ImmutableList.of(new ContentRestriction().setReadOnly(true).setOwnerRestricted(true).setReason("Finalized contract."));

File response = driveService.files().update("FILE_ID", updatedFile).setFields("contentRestrictions").execute();

Python

content_restriction = {'readOnly': True, 'ownerRestricted': True, 'reason':'Finalized contract.'}

response = drive_service.files().update(fileId="FILE_ID", body = {'contentRestrictions' : [content_restriction]}, fields = "contentRestrictions").execute();

Node.js

/**
* Set an owner restricted content restriction on a file.
* @return{obj} updated file
**/
async function addOwnerRestrictedContentRestriction() {
  // 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 contentRestriction = {
    'readOnly': True,
    'ownerRestricted': True,
    'reason': 'Finalized contract.',
  };
  const updatedFile = {
    'contentRestrictions': [contentRestriction],
  };
  try {
    const response = await service.files.update({
      fileId: 'FILE_ID',
      resource: updatedFile,
      fields: 'contentRestrictions',
    });
    return response;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

FILE_ID 替换为您要修改的文件的 fileId

当您运行示例代码时,该文件会受内容限制,只有文件所有者才能将其移除。如果您是文件所有者, 云端硬盘界面 (UI) 中的文件名旁边会显示一个有效锁符号 ()。如果您不是所有者,则锁形符号会变暗。

如需移除 ownerRestricted 标志,请使用 files.update 方法,并将 contentRestrictions.ownerRestricted 字段设置为 false

内容限制功能

files 资源包含一系列布尔值 capabilities 字段,用于表示是否可以对文件执行操作。

内容限制包含以下 capabilities

  • capabilities.canModifyEditorContentRestriction:当前用户是否可以添加或修改内容限制
  • capabilities.canModifyOwnerContentRestriction:当前用户是否可以添加或修改所有者内容限制
  • capabilities.canRemoveContentRestriction:当前用户是否可以移除已应用的内容限制(如果存在)。

如需了解详情,请参阅功能

如需查看检索文件 capabilities 的示例,请参阅验证用户权限

禁止用户下载、打印或复制您的文件

您可以限制具有 role=commenterrole=reader 权限的用户下载、打印和复制云端硬盘、文档、表格和幻灯片中的文件。

如需移除下载、打印和复制文件的选项,请使用 files.update 方法,并将 copyRequiresWriterPermission 布尔值字段设置为 true