Google 雲端硬碟 API 支援多種防止檔案遭修改的方式,包括限制檔案內容,以及禁止下載、列印或複製檔案。
透過雲端硬碟內容限制將檔案設為唯讀
您可以在 Google 雲端硬碟檔案中新增內容限制,禁止使用者執行下列操作:
- 修改標題
- 編輯內容
- 上傳修訂版本
- 新增或修改註解
內容限制與存取限制不同。使用者無法修改檔案內容,但仍可根據存取層級執行其他作業。舉例來說,具備編輯權限的使用者仍可移動項目或變更共用設定。
如要新增或移除雲端硬碟檔案的內容限制,使用者必須具備相關聯的permissions
。如要對「我的雲端硬碟」或共用雲端硬碟中的檔案或資料夾使用 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 表單
- 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. 移除文件中的檔案內容限制。
檢查是否有內容限制
如要檢查內容限制,請搭配 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
的範例,請參閱「取得檔案功能」。
禁止使用者下載、列印或複製你的檔案
您可以限制使用者在雲端硬碟、文件、試算表和簡報中下載、列印及複製檔案的方式。
如要判斷使用者是否可以變更檔案的擁有者或主辦人套用的下載限制,請檢查 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
。如果檔案設定、共用雲端硬碟設定或資料遺失防護規則 (包括含有情境的規則) 對應的角色設有任何下載或複製限制,則值會設為 true
,否則為 false
。
回溯相容性
建議您使用 DownloadRestriction
物件,強制規定使用者下載、列印及複製檔案的方式。
如要使用 copyRequiresWriterPermission
布林值欄位,從欄位讀取和寫入欄位的功能有所不同。
copyRequiresWriterPermission
欄位的擷取值會反映具有 role=commenter
或 role=reader
權限的使用者,是否可以下載、列印或複製雲端硬碟中的檔案。欄位值會反映檔案設定、共用雲端硬碟設定或資料遺失防護規則的組合。不過,這項功能不包含資料遺失防護規則的內容評估。
將 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 |