Conflict detection while uploading a document to Google Drive

My Android app updates a document in Google Drive. The file can be modified in another place (for example, through the web interface of the drive), so there may be a conflict when downloading the file. However, this rarely happens. Therefore, I do not want my application to request a change history first (since this is not necessary in most cases), and only then update the file. How can I detect that there is a conflict when updating a file?

So far, my research shows that getHeadRevisionId () returns null , although the version ID with a zero head was fixed as fixed . Another thing I tried is setEtag () in the file before updating (). This should have given me an error while updating , but the download was successful, even the file was deleted remotely! Is it right to use ETag?

0
source share
2 answers

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 at the same time, the update will be 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" } 

Please note that the ETag may change even if the content is gone .

+1
source

"Is it right to use ETag?"

Yes

In addition, for files other than Docs, you should also check md5Checksum for content changes.

+1
source

Source: https://habr.com/ru/post/1496521/


All Articles