Reputation: 654
I'm trying to figure out an efficient way to copy a Google Drive file so that the copy lives only in the user's root folder.
I know that when you create a new Google Drive file, by default it lives in the root folder:
>>> body = {"title": "Test Document", "mimeType": "application/vnd.google-apps.document"}
>>> document = drive_service.files().insert(body=body).execute()
>>> document["parents"]
[{u'id': u'...', u'isRoot': True, 'kind': u'drive#parentReference', ...}]
However, when you copy a Google Drive file, by default it lives in the same folder(s) as the source.
One possibility is to first query the ID of the user's root folder and pass that into the copy request:
>>> about = drive_service.about().get().execute()
>>> root_id = about["rootFolderId"]
>>> body = {"title": "My copy", "parents": [{"id": root_id}]}
>>> copied_document = drive_service.files().copy(fileId="SOMEID", body=body).execute()
However, the above requires an extra query and increases the overall time to complete the operation.
I'm searching for a way to do this without knowing the root folder ID ahead of time. I've tried passing an empty list to "parents", but that still causes the copy to be placed in the same folder(s):
>>> body = {"title": "My copy", "parents": []}
>>> copied_document = drive_service.files().copy(fileId="SOMEID", body=body).execute()
I also tried passing in a "stub" for the root folder, but that threw an error:
>>> body = {"title": "My copy", "parents": [{"isRoot": True, "kind": "drive#parentReference"}]}
>>> copied_document = drive_service.files().copy(fileId="SOMEID", body=body).execute()
Traceback (most recent call last)
...
HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/SOMEID/copy?alt=json returned "File not found:">
Is there another method I'm missing?
(The above examples use the Google API Python Client)
Upvotes: 1
Views: 356
Reputation: 22306
I seem to recall that the folder ID of root is aliased to "root". Try passing that as the root id in your second approach.
Upvotes: 4