Google Drive API 支援多種防止檔案修改的方式,包括限制檔案內容,以及禁止下載、列印或複製檔案的選項。
設定雲端硬碟內容限制,將檔案設為唯讀
您可以在 Google 雲端硬碟檔案中加入內容限制,防止使用者執行以下操作:
- 修改標題
- 編輯內容
- 上傳修訂版本
- 新增或修改註解
套用內容限制是一種將雲端硬碟項目內容設為唯讀的機制,不必變更項目的存取權權限。也就是說,這沒有存取權限制。雖然使用者無法修改檔案內容,但系統仍支援根據存取層級進行其他作業 (例如,擁有編輯權限的使用者仍可移動項目或變更共用設定)。
如要新增或移除雲端硬碟檔案的內容限制,使用者必須具備相關聯的權限。對於「我的雲端硬碟」中的檔案或資料夾,或是具有 capabilities.canModifyEditorContentRestriction
的共用雲端硬碟,您必須指派 role=writer
。如果是「我的雲端硬碟」中的檔案或資料夾,或是設有 ownerRestricted
內容限制的共用雲端硬碟,您必須擁有或具備 role=organizer
檔案。如要查看設有內容限制的項目,使用者必須具有 role=reader
以上版本。如需完整的角色清單,請參閱角色與權限一文。如要變更檔案權限,請參閱變更權限。
您可以使用 files
資源的 contentRestrictions.readOnly
布林值欄位來設定內容限制。請注意,設定項目的內容限制會覆寫現有的項目。
內容限制的情境
只要針對雲端硬碟項目設定內容限制,使用者就會知道不應變更內容。可能的原因如下:
- 在檢閱或稽核期間暫停協作文件。
- 將項目設為最終狀態,例如「已核准」。
- 防止敏感會議期間發生異動。
- 禁止對自動化系統處理的工作流程進行外部變更。
- 透過 Google Apps Script 和 Google Workspace 外掛程式限制編輯內容。
- 避免意外編輯文件。
請注意,雖然內容限制有助於管理內容,但並不能防止具有足夠權限的使用者繼續處理項目。此外,也不是建立不可變更記錄的方法。雲端硬碟內容限制可以變動,因此即使項目設有內容限制,也無法保證該項目永遠不會變更。
管理有內容限制的檔案
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
。
執行程式碼範例時,檔案不再受到限制。
您也可以使用雲端硬碟 UI 移除內容限制,以及允許內容編輯 (前提是您已擁有適當的權限)。方法有兩種:
在雲端硬碟中,在含有內容限制的檔案上按一下滑鼠右鍵,然後按一下「Unlock
」。開啟設有內容限制的檔案,然後依序點選「(Locked mode)」>「解鎖檔案」。
檢查內容限制
如要檢查內容限制,請使用 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
。
執行程式碼範例時,檔案會受到內容限制,只有檔案擁有者才能移除該檔案。如果您是檔案擁有者, 雲端硬碟使用者介面 (UI) 中的檔案名稱旁邊會顯示使用中的鎖頭符號 ( )。如果您不是擁有者,鎖頭符號會變暗。
如要移除 ownerRestricted
標記,請使用 files.update
方法,並將 contentRestrictions.ownerRestricted
欄位設為 false
。
內容限制功能
files
資源包含一組布林值 capabilities
欄位,用於表示是否可以在檔案上執行動作。
內容限制包含下列capabilities
:
capabilities.canModifyEditorContentRestriction
:目前使用者是否能新增或修改內容限制。capabilities.canModifyOwnerContentRestriction
:目前使用者是否能新增或修改擁有者內容限制。capabilities.canRemoveContentRestriction
:目前使用者是否能夠移除已套用的內容限制 (如有)。
詳情請參閱「功能」一文。
如需擷取 capabilities
檔案的範例,請參閱「驗證使用者權限」。
禁止使用者下載、列印或複製你的檔案
您可以限制擁有 role=commenter
或 role=reader
權限的使用者,以何種方式下載、列印及複製雲端硬碟、文件、試算表和簡報中的檔案。
如要移除下載、列印和複製檔案的選項,請使用 files.update
方法,並將 copyRequiresWriterPermission
布林值欄位設為 true
。