Google Drive API では、ファイルのコンテンツの制限や、ファイルのダウンロード、印刷、コピーのオプションの禁止など、ファイルの変更を防ぐためのいくつかの方法がサポートされています。
ドライブのコンテンツの制限を使用してファイルを読み取り専用にする
Google ドライブ ファイルにコンテンツの制限を追加すると、ユーザーは次の操作を行えなくなります。
- タイトルを変更する
- コンテンツを編集する
- リビジョンをアップロードする
- コメントを追加または変更する
コンテンツの制限はアクセス制限ではありません。ユーザーはファイルのコンテンツを変更できませんが、アクセスレベルに応じて他の操作は引き続き許可されます。 たとえば、編集権限を持つユーザーは、アイテムを移動したり、共有設定を変更したりできます。
ドライブ内のファイルにコンテンツの制限を追加または削除するには、ユーザー
は関連する permissionsが必要です。capabilities.canModifyEditorContentRestriction を持つマイドライブまたは共有ドライブ内のファイルやフォルダの場合は、role=writer が割り当てられている必要があります。ownerRestricted コンテンツの制限があるマイドライブまたは共有ドライブ内のファイルやフォルダの場合は、ファイルのオーナーであるか、role=organizer が必要です。コンテンツの制限があるアイテムを表示するには、role=reader 以上の権限が必要です。ロールの一覧については、ロールと
権限をご覧ください。ファイルの権限を更新するには、
権限を更新するをご覧ください。
contentRestrictions.readOnly ブール値フィールドを files リソースで使用して、コンテンツ
の制限を設定できます。アイテムにコンテンツの制限を設定すると、既存の制限が上書きされます。
コンテンツの制限のシナリオ
ドライブ アイテムのコンテンツの制限は、コンテンツを変更しないようにユーザーに通知します。これには、次のような理由が考えられます。
- レビュー期間または監査期間中に、共同編集ドキュメントの作業を一時停止する。
- アイテムを承認済みなどの最終状態に設定する。
- 機密性の高い会議中に変更を防ぐ。
- 自動化されたシステムで処理されるワークフローの外部変更を禁止する。
- Google Apps Script と Google Workspace アドオンによる編集を制限する。
- ドキュメントの誤編集を防ぐ。
コンテンツの制限はコンテンツの管理に役立ちますが、十分な権限を持つユーザーがアイテムの作業を続行することを防ぐものではありません。また、不変のレコードを作成する方法でもありません。 ドライブのコンテンツの制限は変更可能であるため、アイテムのコンテンツの制限によってアイテムが変更されないことが保証されるわけではありません。
コンテンツの制限があるファイルを管理する
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)内のファイル名の横にロック記号 ()が表示されます。これでファイルは読み取り専用になります。
コンテンツの制限を削除する
ファイルのコンテンツの制限を削除するには、contentRestrictions.readOnly フィールドを false に設定して files.update メソッドを使用します。次のコードサンプルは、コンテンツの制限を削除する方法を示しています。
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 を使用して、コンテンツの制限を削除し、コンテンツの編集を許可することもできます(適切な権限がある場合)。これを行うには、次の 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 フラグを削除するには、
contentRestrictions.ownerRestricted フィールドを false に設定して files.update メソッドを使用します。
コンテンツの制限機能
files リソースには、ファイルに対してアクションを実行できるかどうかを示すブール値の
capabilities フィールドのコレクションが含まれています。
コンテンツの制限には、次の capabilities が含まれます。
capabilities.canModifyEditorContentRestriction: 現在のユーザーが コンテンツの制限を追加または変更できるかどうか。capabilities.canModifyOwnerContentRestriction: 現在のユーザーがオーナーのコンテンツの制限を追加または変更できるかどうか。capabilities.canRemoveContentRestriction: 現在のユーザーが適用されたコンテンツの制限を 削除できるかどうか(存在する場合)。
詳細については、ファイルの 機能についてをご覧ください。
ファイルの capabilities を取得する例については、ファイルの機能を取得するをご覧ください。
ユーザーがファイルをダウンロード、印刷、コピーできないようにする
ドライブ、ドキュメント、スプレッドシート、スライド内でユーザーがファイルをダウンロード、印刷、コピーする方法を制限できます。
ユーザーがファイルのオーナーまたはオーガナイザーによって適用されたダウンロード
制限を変更できるかどうかを確認するには、
capabilities.canChangeItemDownloadRestriction ブール値フィールドを確認します。capabilities.canChangeItemDownloadRestriction が true に設定されている場合は、ファイルにダウンロード制限を適用できます。詳細については、ファイルの機能について
をご覧ください。
ファイルにダウンロード制限を適用するには、downloadRestrictions フィールドを使用して
files.update メソッドを設定します。フィールドを設定できます
を使用して
DownloadRestrictionsMetadata
オブジェクト。
DownloadRestrictionsMetadata オブジェクトには、itemDownloadRestriction と effectiveDownloadRestrictionWithContext の 2 つのフィールドがあります。どちらのフィールドも読み取り可能ですが、設定できるのは itemDownloadRestriction のみです。
itemDownloadRestriction フィールドは DownloadRestriction オブジェクトを返します。DownloadRestriction オブジェクトには、restrictedForReaders と restrictedForWriters の 2 つの個別のブール値フィールドがあります。
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 のみ |