Google 云端硬盘 API 支持多种防止文件修改的方法,包括限制文件内容和禁止下载、打印或复制文件的选项。
通过云端硬盘内容限制将文件设为只读
您可以向 Google 云端硬盘文件添加内容限制,以防止用户执行以下操作:
- 修改标题
- 修改内容
- 上传修订版本
- 添加或修改注释
内容限制并非访问权限限制。虽然用户无法修改文件的内容,但仍可根据其访问权限级别执行其他操作。例如,拥有修改权限的用户仍然可以移动内容或更改其共享设置。
如需在云端硬盘中添加或移除文件的内容限制,用户必须拥有关联的permissions
。对于“我的云端硬盘”或共享云端硬盘中带有 capabilities.canModifyEditorContentRestriction
的文件或文件夹,您必须拥有 role=writer
权限。对于“我的云端硬盘”或共享云端硬盘中具有 ownerRestricted
内容限制的文件或文件夹,您必须是相应文件的所有者,或者拥有 role=organizer
。如需查看受内容限制的商品,用户必须拥有 role=reader
或更高版本的设备。如需查看完整的角色列表,请参阅角色和权限。如需更新文件权限,请参阅更新权限。
您可以使用 files
资源中的 contentRestrictions.readOnly
布尔值字段来设置内容限制。请注意,为内容设置限制会覆盖现有限制。
内容限制的应用场景
云端硬盘内容限制会向用户表明,相应内容不应更改。这可能是出于以下几个原因:
- 在审核期间暂停协作处理文档。
- 将商品设置为最终状态,例如“已批准”。
- 防止在敏感会议期间进行更改。
- 禁止对由自动化系统处理的工作流进行外部更改。
- 限制 Google Apps 脚本和 Google Workspace 插件的编辑权限。
- 避免意外编辑文档。
不过请注意,虽然内容限制有助于管理内容,但其目的并不是阻止具有足够权限的用户继续处理某个项目。此外,它也不是创建不可变记录的方式。 云端硬盘内容限制是可变的,因此对某个项目施加内容限制并不能保证该项目永远不会更改。
管理受内容限制的文件
Google 文档、Google 表格和 Google 幻灯片以及所有其他文件都可能包含内容限制。
对商品施加内容限制后,将无法更改其标题和内容,包括:
- 评论和建议(针对 Google 文档、表格、幻灯片和二进制文件)
- 二进制文件的修订版本
- Google 文档中的文本和格式
- 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) 中的文件名旁边会显示一个锁定符号 ( )。相应文件目前处于只读状态。
移除内容限制
如需移除文件内容限制,请使用 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
。
运行示例代码后,相应文件不再受内容限制。
您还可以使用 Google 云端硬盘界面移除内容限制并允许内容编辑(前提是您拥有相应的正确权限)。您可以通过以下两种方式执行此操作:
在云端硬盘中,右键点击受内容限制的文件,然后点击解锁
。图 2. 移除云端硬盘文件列表中的文件内容限制。 打开受内容限制的文件,然后依次点击(锁定模式)> 解锁文件。
图 3. 移除文档中的文件内容限制。
检查是否存在内容限制
如需检查内容限制,请使用 files.get
方法和返回的 contentRestrictions
字段。以下代码示例展示了如何检查内容限制的状态:
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
。
运行示例代码后,相应文件会受到内容限制,只有文件所有者才能移除该限制。如果您是文件所有者,则在 Google 云端硬盘界面 (UI) 中,文件名旁边会显示一个已启用的锁定符号 ( )。如果您不是所有者,锁形符号会变暗。
如需移除 ownerRestricted
标志,请使用 files.update
方法并将 contentRestrictions.ownerRestricted
字段设置为 false
。
内容限制功能
files
资源包含一组布尔值 capabilities
字段,用于指示是否可以对文件执行操作。
内容限制包含以下 capabilities
:
capabilities.canModifyEditorContentRestriction
:当前用户是否可以添加或修改内容限制。capabilities.canModifyOwnerContentRestriction
:当前用户是否可以添加或修改所有者内容限制。capabilities.canRemoveContentRestriction
:当前用户是否可以移除已应用的内容限制(如果有)。
如需了解详情,请参阅了解文件功能。
如需查看检索文件 capabilities
的示例,请参阅获取文件权能。
禁止用户下载、打印或复制您的文件
您可以限制用户在云端硬盘、Google 文档、Google 表格和 Google 幻灯片中下载、打印和复制文件的方式。
如需确定用户是否可以更改文件所有者或组织者设置的下载限制,请检查 capabilities.canChangeItemDownloadRestriction
布尔值字段。如果将 capabilities.canChangeItemDownloadRestriction
设置为 true
,则可以对文件应用下载限制。如需了解详情,请参阅了解文件功能。
如需对文件应用下载限制,请使用 files.update
方法设置 downloadRestrictions
字段。您可以使用 DownloadRestrictionsMetadata
对象设置相应字段。
DownloadRestrictionsMetadata
对象包含两个字段:itemDownloadRestriction
和 effectiveDownloadRestrictionWithContext
。这两个字段均可读取,但只有 itemDownloadRestriction
可设置。itemDownloadRestriction
字段会返回一个 DownloadRestriction
对象。DownloadRestriction
对象包含两个单独的布尔值字段:restrictedForReaders
和 restrictedForWriters
。
设置 itemDownloadRestriction
字段时,文件所有者或组织者会直接应用文件下载限制。它不会考虑共享云端硬盘设置或数据泄露防护 (DLP) 规则。如需了解详情,请参阅 DLP 简介。
如果您通过将 restrictedForWriters
字段设置为 true
来更新 itemDownloadRestriction
字段,则表示 restrictedForReaders
为 true
。同样,将 restrictedForWriters
设置为 true
并将 restrictedForReaders
设置为 false
,相当于将 restrictedForWriters
和 restrictedForReaders
都设置为 true
。
对于 effectiveDownloadRestrictionWithContext
字段,下载限制适用于相应文件,并会考虑所有限制设置和 DLP 规则。
effectiveDownloadRestrictionWithContext
字段可以设置为 restrictedForWriters
或 restrictedForReaders
。如果文件设置、共享云端硬盘设置或 DLP 规则(包括具有上下文的规则)中针对相应角色设置了任何下载或复制限制,则该值设置为 true
,否则设置为 false
。
向后兼容性
我们建议您使用 DownloadRestriction
对象来强制规定用户下载、打印和复制文件的方式。
如果您想使用 copyRequiresWriterPermission
布尔值字段,那么从该字段读取数据和向该字段写入数据的功能会有所不同。
检索到的 copyRequiresWriterPermission
字段值反映了具有 role=commenter
或 role=reader
权限的用户是否可以下载、打印或复制云端硬盘中的文件。该字段值反映了文件设置、共享云端硬盘设置或 DLP 规则的组合。不过,不包括 DLP 规则的上下文评估。
将 copyRequiresWriterPermission
字段设置为 false
会将 restrictedForWriters
和 restrictedForReaders
字段都更新为 false
。这意味着所有用户的下载或复制限制设置都会被移除。
用于控制下载、打印和复制功能的字段
下表列出了影响下载、打印和复制功能的 files
资源字段:
字段 | 说明 | 版本 |
---|---|---|
capabilities.canCopy |
当前用户是否可以复制文件。 | v2 和 v3 |
capabilities.canDownload |
当前用户是否可以下载文件。 | v2 和 v3 |
capabilities.canChangeCopyRequiresWriterPermission |
当前用户是否可以更改文件的 copyRequiresWriterPermission 限制。 |
v2 和 v3 |
capabilities.canChangeItemDownloadRestriction |
当前用户是否可以更改文件的下载限制。 | 仅限 v3 |
copyRequiresWriterPermission |
是否应禁止读者和评论者复制、打印或下载此文件。 | v2 和 v3 |
downloadRestrictions |
应用于文件的下载限制。 | 仅限 v3 |