Detect Conflict When Uploading Document To Google Drive
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
Solution 1:
Set the If-Match
HTTP header:
finalUpdateupdate= 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.
Solution 2:
"Is this the correct way to use ETag?"
Yes
Also, for non-Docs files, you should also check md5Checksum for changes to the content.
Post a Comment for "Detect Conflict When Uploading Document To Google Drive"