ファイル コンテンツを保護する

Google Drive API は、ファイル コンテンツの制限や、ファイルのダウンロード、印刷、コピーを禁止するなど、ファイルの変更を防止するいくつかの方法をサポートしています。

ドライブのコンテンツの制限を使用してファイルを読み取り専用にする

Google ドライブ ファイルにコンテンツの制限を追加すると、ユーザーが次の操作を行えなくなります。

  • タイトルの変更
  • コンテンツの編集
  • リビジョンのアップロード
  • コメントの追加または変更

コンテンツの制限の適用は、アイテムのアクセス権を変更せずに、ドライブのアイテムのコンテンツを読み取り専用にできるメカニズムです。これはアクセス制限ではないということです。ユーザーはファイルの内容を変更できませんが、アクセスレベルに基づいてその他の操作は引き続き可能です(たとえば、編集権限を持つユーザーは、アイテムの移動や共有設定の変更などが可能です)。

ドライブ内のファイルに対するコンテンツ制限を追加または削除するには、関連する権限がユーザーに付与されている必要があります。マイドライブ内のファイルまたはフォルダ、または capabilities.canModifyEditorContentRestriction がある共有ドライブの場合、role=writer が割り当てられている必要があります。マイドライブ内のファイルまたはフォルダ、または ownerRestricted のコンテンツ制限のある共有ドライブの場合、そのファイルを所有しているか、role=organizer を持っている必要があります。コンテンツの制限があるアイテムを表示するには、ユーザーは role=reader 以上が必要です。ロールの完全なリストについては、ロールと権限をご覧ください。ファイルの権限を変更するには、権限を変更するをご覧ください。

コンテンツの制限を設定するには、files リソースの contentRestrictions.readOnly ブール値フィールドを使用します。アイテムにコンテンツの制限を設定すると、既存のアイテムが上書きされることに注意してください。

コンテンツの制限に関するシナリオ

ドライブのアイテムに対するコンテンツの制限は、コンテンツを変更しないことをユーザーに示すものです。これには、次のような理由が考えられます。

  • 審査または監査期間中は、共同編集しているドキュメントの作業を一時停止する。
  • アイテムを最終状態(承認済みなど)に設定する。
  • 機密性が高い会議中の変更を防止する。
  • 自動システムによって処理されるワークフローの外部変更を禁止する。
  • Google Apps Script と Google Workspace アドオンによる編集の制限
  • ドキュメントの誤編集の防止

なお、コンテンツの制限はコンテンツの管理に役立ちますが、十分な権限を持つユーザーがアイテムの作業を続行できないようにするものではありません。また、不変レコードの作成方法でもありません。ドライブのコンテンツ制限は変更可能です。したがって、アイテムに対するコンテンツ制限によって、そのアイテムが変更されないとは限りません。

コンテンツの制限があるファイルを管理する

Google ドキュメント、Google スプレッドシート、Google スライド、その他のすべてのファイルには、コンテンツの制限が含まれている場合があります。

アイテムのコンテンツを制限すると、次のようなタイトルとコンテンツを変更できなくなります。

  • コメントと提案(ドキュメント、スプレッドシート、スライド、バイナリ ファイル)
  • バイナリ ファイルのリビジョン
  • ドキュメントのテキストと書式設定
  • スプレッドシート内のテキストまたは数式、 スプレッドシートのレイアウト、スプレッドシートのインスタンス
  • スライド内のすべてのコンテンツ、スライドの順序と数

一部のファイル形式にはコンテンツの制限を含めることはできません。以下に例を示します。

コンテンツの制限を追加する

ファイルのコンテンツの制限を追加するには、contentRestrictions.readOnly フィールドを true に設定して files.update メソッドを使用します。制限を追加する理由(「確定済みの契約」など)をオプションで追加します。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. ドライブ内のファイルリスト内にコンテンツの制限があるファイル。

コンテンツの制限を解除する

ファイルのコンテンツ制限を削除するには、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 つの方法があります。

  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 リソースを返します(存在する場合)。

ファイルのオーナーのみが変更できるコンテンツの制限を追加する

ファイルのコンテンツの制限を追加して、ファイルのオーナーだけがメカニズムを切り替えられるようにするには、contentRestrictions.ownerRestricted ブール値フィールドを true に設定して files.update メソッドを使用します。次のコードサンプルは、ファイルのオーナーのみにコンテンツの制限を追加する方法を示しています。

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 の取得例については、ユーザー権限を確認するをご覧ください。

ユーザーがファイルをダウンロード、印刷、コピーできないようにする

role=commenter または role=reader の権限を持つユーザーが、ドライブ、ドキュメント、スプレッドシート、スライド内でファイルをダウンロード、印刷、コピーする方法を制限できます。

ファイルのダウンロード、印刷、コピーのオプションを削除するには、copyRequiresWriterPermission ブール値フィールドを true に設定して files.update メソッドを使用します。