Detect conflict when uploading document to Google Drive

荒凉一梦 提交于 2019-12-02 17:54:23

问题


My Android app updates a Google Drive document. The file can be modified also elsewhere (e.g. through Drive web interface), so there may be a conflict on file upload. However, this should rarely happen. That's why I don't want my app to first query the revision history (since this is in most cases unnecessary) and only after that, update the file. How can I detect that there is a conflict when updating the file?

My investigation so far reveals that getHeadRevisionId() returns null although the null head revision id has been reported fixed. Another thing I tried was setEtag() on the file before update(). It should have given me error on update, but the upload was successful even the file had been changed remotely! Is this the correct way to use ETag?


回答1:


"Is this the correct way to use ETag?"

Yes

Also, for non-Docs files, you should also check md5Checksum for changes to the content.




回答2:


Set the If-Match HTTP header:

final Update update = mService.files().update(mDriveFile.getId(), mDriveFile, byteContent);
final HttpHeaders headers = update.getRequestHeaders();
headers.setIfMatch(mDriveFile.getEtag());
update.setRequestHeaders(headers);
mDriveFile = update.execute();

In case the document has changed meanwhile, the update will get rejected with a response something like:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 412 Precondition Failed
{
  "code": 412,
  "errors": [
    {
      "domain": "global",
      "location": "If-Match",
      "locationType": "header",
      "message": "Precondition Failed",
      "reason": "conditionNotMet"
    }
  ],
  "message": "Precondition Failed"
}

Note that ETag might change even if the content does not.



来源:https://stackoverflow.com/questions/21793871/detect-conflict-when-uploading-document-to-google-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!