I am writing a function to copy a folder in my Google Drive to another folder. The function works for folders that are in the regular Drive space. However, when I run it on a team drive folder, I am getting the below error.
Traceback (most recent call last):
File "C:/Code/catpython/driveapi_utils.py", line 232, in <module>
test_copy_folder()
File "C:/Code/catpython/driveapi_utils.py", line 221, in test_copy_folder
copy_folder(service, src='xxxxxx',
File "C:/Code/catpython/driveapi_utils.py", line 211, in copy_folder
copy_folder(service, file.get('id'), file.get('name'), dst_parent_id, **kwargs)
File "C:/Code/catpython/driveapi_utils.py", line 201, in copy_folder
fileId=clone.get("id"),
AttributeError: 'NoneType' object has no attribute 'get'
My code is as follows:
def copy_folder(service, src, src_name, dst, **kwargs):
"""
Copies a folder. It will create a new folder in dst with the same name as src,
and copies the contents of src into the new folder
src: Source folder's id
dst: Destination folder's id that the source folder is going to be copied to
"""
page_token = None
while True:
response = service.files().list(q="'%s' in parents and trashed = false" % src,
supportsAllDrives=True,
spaces='drive',
fields='nextPageToken, files(id, name, mimeType)',
pageToken=page_token,
includeItemsFromAllDrives=True,
).execute()
dst_parent_id = create_folder(service, src_name, dst, **kwargs)
for file in response.get('files', []):
# Process change
print('Found file: %s (%s, %s)' % (file.get('name'), file.get('id'), file.get('mimeType')))
if not file.get('mimeType') == 'application/vnd.google-apps.folder':
clone = None
try:
clone = service.files().copy(fileId=file.get('id'),
body={'title': 'copiedFile',
'parents': [{'kind': "drive#fileLink",
'id': dst_parent_id}],
'supportsAllDrives': True,
}).execute()
except HttpError as httpe:
pass
try:
service.files().update(
fileId=clone.get("id"),
addParents=dst_parent_id,
removeParents=src,
fields="id, parents",
supportsAllDrives=True
).execute()
except HttpError as httpe:
pass
else:
print('drilling into %s' % file.get('name'))
copy_folder(service, file.get('id'), file.get('name'), dst_parent_id, **kwargs)
page_token = response.get('nextPageToken', None)
if page_token is None:
break
I debugged the code and I think the copy api threw an error:
<HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/1n7h04M6Rpz3m2J6MGZq8trlnADEBFLrK/copy?alt=json returned "File not found: 1n7h04M6Rpz3m2J6MGZq8trlnADEBFLrK.". Details: "File not found: 1n7h04M6Rpz3m2J6MGZq8trlnADEBFLrK.">
However the file does exist and the same code works on files in my personal space. I have full access to the team drive, as you can see from the code, I am able to query files and create folders in it.
What could be missing? Or is there a better way to implement this?