Protect file content from modification

You can use the contentRestrictions.readOnly field on a files resource to lock a file and prevent people from modifying the title, uploading a revision, or adding comments.

Lock file content

To lock file content, use the files.update method with the contentRestrictions.readOnly field set to true. Add an optional reason for why you're locking the file, such as "Finalized contract." The following shows how to lock file content:

Python

service.files().update(fileId=fileId,
  body={"contentRestrictions":
      [{"readOnly": "true", "reason": "Finalized contract."}]
  })

Unlock file content

To unlock file content, use the files.update method with the contentRestrictions.readOnly field set to false. The following shows how to unlock file content:

Python

service.files().update(fileId=fileId,
  body={"contentRestrictions":
      [{"readOnly": "false"}]
  })

Check a file lock

To check a file lock, use the files.get method with the contentRestrictions returned field. The following shows how to check the status of a file lock:

Python

service.files()
  .get(fileId=fileId, fields="contentRestrictions").execute()