1

v3

I'd simply like to update a single Doc on the user's drive.

But in order to do this, it seems I need to download the file first, then edit the doc, and finally upload it. I'm thinking I also need to delete the old file, in order to upload the edited file under the same name.

This all seems a bit overcomplicated.

With the google drive API, how can I directly edit an existing file on the user's drive?

I found documents and code examples for downloading files, and uploading files, but the docs for updating files has me a little lost as the "required query parameter" links back to the upload page...

Does this mean that I can use the same code as an upload?:

file_metadata = {'name': 'essay.doc'}
media = MediaFileUpload('files/essay.doc',
                        mimetype='text/plain')
file = drive_service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
print 'File ID: %s' % file.get('id')  

If that's the case, where does the logic to check for an existing file come in? How does it handle uploading a file when that file already exists in the drive?

I'm simply trying to check that a file on the drive exists, create one if it doesn't, then append a bunch of text to the content of the document.

Jay Jung
  • 1,805
  • 3
  • 23
  • 46
  • AFAIK, yes, you'll just have to change the endpoint from `create()` to `update()`. This will just perform a patch update to the file (doesn't need to delete old file). See related SO posts: [1](https://stackoverflow.com/a/41467186/5995040) and [2](https://stackoverflow.com/a/38595980/5995040). Hope this helps. – Mr.Rebot Mar 28 '18 at 18:28
  • @Mr.Rebot where would I be uploading the edited content? there doesn't appear to be any query parameters for that purpose outlined in the update docs. Seems only to have fields related to metadata or content hints(?) – Jay Jung Mar 29 '18 at 14:16
  • This will still be placed on the media_body for the upload purposes, just like create function. see link for [details](https://developers.google.com/resources/api-libraries/documentation/drive/v2/python/latest/drive_v2.files.html#update) – Mr.Rebot Mar 29 '18 at 15:48

0 Answers0