使用 Google-Drive-API 移动文件
Move a file with Google-Drive-API
是否可以通过 python 的 googleapi 客户端模块显式移动文件?给定文件、原始路径和目标路径,我想创建以下函数:
def move_file(service, filename, init_drive_path, drive_path, copy=False):
"""Moves a file in Google Drive from one location to another.
service: Drive API service instance.
filename (string): full name of file on drive
init_drive_path (string): initial file location on Drive
drive_path (string): the file path to move the file in on Drive
copy (boolean): file should be saved in both locations
Returns nothing.
"""
目前我一直在通过手动下载文件然后将其重新上传到所需位置来执行此操作,但这对于大文件来说并不实用,而且似乎是一种变通方法。
Here's the documentation 用于 google-驱动器-api.
上可用的方法
编辑 请参阅下面的解决方案:
Found it here.您只需要检索文件和文件夹ID,然后使用更新方法。如果您想在旧文件夹中保留文件副本,则可以排除 remove_parents 参数
file_id = '***'
folder_id = '***'
# Retrieve the existing parents to remove
file = drive_service.files().get(fileId=file_id, fields='parents').execute()
previous_parents = ",".join(file.get('parents'))
# Move the file to the new folder
file = drive_service.files().update(
fileId=file_id,
addParents=folder_id,
removeParents=previous_parents,
fields='id, parents'
).execute()
(注意我没有包括我的基本辅助函数 _getFileId 和 _getFolderId)所以我的原始函数看起来像:
def move_file(service, filename, init_drive_path, drive_path, copy=False):
"""Moves a file in Google Drive from one location to another.
service: Drive API service instance.
'filename' (string): file path on local machine, or a bytestream
to use as a file.
'init_drive_path' (string): the file path the file was initially in on Google
Drive (in <folder>/<folder>/<folder> format).
'drive_path' (string): the file path to move the file in on Google
Drive (in <folder>/<folder>/<folder> format).
'copy' (boolean): file should be saved in both locations
Returns nothing.
"""
file_id = _getFileId(service, filename, init_drive_path)
folder_id = _getFolderId(service, drive_path)
if not file_id or not folder_id:
raise Exception('Did not find file specefied: {}/{}'.format(init_drive_path, filename))
file = service.files().get(fileId=file_id, fields='parents').execute()
if copy:
previous_parents = ''
else:
previous_parents = ",".join(file.get('parents'))
file = drive_service.files().update(
fileId=file_id,
addParents=folder_id,
removeParents=previous_parents,
fields='id, parents'
).execute()
是否可以通过 python 的 googleapi 客户端模块显式移动文件?给定文件、原始路径和目标路径,我想创建以下函数:
def move_file(service, filename, init_drive_path, drive_path, copy=False):
"""Moves a file in Google Drive from one location to another.
service: Drive API service instance.
filename (string): full name of file on drive
init_drive_path (string): initial file location on Drive
drive_path (string): the file path to move the file in on Drive
copy (boolean): file should be saved in both locations
Returns nothing.
"""
目前我一直在通过手动下载文件然后将其重新上传到所需位置来执行此操作,但这对于大文件来说并不实用,而且似乎是一种变通方法。
Here's the documentation 用于 google-驱动器-api.
上可用的方法编辑 请参阅下面的解决方案:
Found it here.您只需要检索文件和文件夹ID,然后使用更新方法。如果您想在旧文件夹中保留文件副本,则可以排除 remove_parents 参数
file_id = '***'
folder_id = '***'
# Retrieve the existing parents to remove
file = drive_service.files().get(fileId=file_id, fields='parents').execute()
previous_parents = ",".join(file.get('parents'))
# Move the file to the new folder
file = drive_service.files().update(
fileId=file_id,
addParents=folder_id,
removeParents=previous_parents,
fields='id, parents'
).execute()
(注意我没有包括我的基本辅助函数 _getFileId 和 _getFolderId)所以我的原始函数看起来像:
def move_file(service, filename, init_drive_path, drive_path, copy=False):
"""Moves a file in Google Drive from one location to another.
service: Drive API service instance.
'filename' (string): file path on local machine, or a bytestream
to use as a file.
'init_drive_path' (string): the file path the file was initially in on Google
Drive (in <folder>/<folder>/<folder> format).
'drive_path' (string): the file path to move the file in on Google
Drive (in <folder>/<folder>/<folder> format).
'copy' (boolean): file should be saved in both locations
Returns nothing.
"""
file_id = _getFileId(service, filename, init_drive_path)
folder_id = _getFolderId(service, drive_path)
if not file_id or not folder_id:
raise Exception('Did not find file specefied: {}/{}'.format(init_drive_path, filename))
file = service.files().get(fileId=file_id, fields='parents').execute()
if copy:
previous_parents = ''
else:
previous_parents = ",".join(file.get('parents'))
file = drive_service.files().update(
fileId=file_id,
addParents=folder_id,
removeParents=previous_parents,
fields='id, parents'
).execute()