首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Google-Drive-API移动文件

使用Google-Drive-API移动文件
EN

Stack Overflow用户
提问于 2016-11-21 17:16:33
回答 1查看 4.9K关注 0票数 5

是否可以通过python的googleapiclient模块显式地移动文件?我希望创建以下函数,给定一个文件、原始路径和目标路径:

代码语言:javascript
复制
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.
    """

目前,我一直通过手动下载文件并将其重新上传到所需位置来执行此操作,但是这对于大文件来说并不实用,而且似乎是一种变通的方法。

这是文件用于google上可用的方法。

编辑--见下面的解决方案:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-21 19:20:38

在这里找到的。只需检索文件和文件夹ID,然后使用update方法即可。如果要将文件的副本保留在旧文件夹中,则可以排除remove_parents参数。

代码语言:javascript
复制
    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),所以我的原始函数看起来如下所示:

代码语言:javascript
复制
    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()
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40725769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档