【问题标题】:Backing Up a folder using Google Drive's API使用 Google Drive 的 API 备份文件夹
【发布时间】:2021-01-07 22:33:18
【问题描述】:

嘿,现在我正在尝试发出一个不和谐的命令,将我的机器人的备份上传到 Google 云端硬盘。到目前为止,我只有这两个功能,但我不确定如何将整个文件夹转移到 Google Drive 文件夹中。

def getFileByteSize(filename):
    # Get file size in python
    from os import stat
    file_stats = stat(filename)
    print('File Size in Bytes is {}'.format(file_stats.st_size))
    return file_stats.st_size

def upload_file(drive_service, filename, mimetype, upload_filename, resumable=True, chunksize=262144):
    media = MediaFileUpload(filename, mimetype=mimetype, resumable=resumable, chunksize=chunksize)
    # Add all the writable properties you want the file to have in the body!
    body = {"name": upload_filename} 
    request = drive_service.files().create(body=body, media_body=media).execute()
    if getFileByteSize(filename) > chunksize:
        response = None
        while response is None:
            chunk = request.next_chunk()
            if chunk:
                status, response = chunk
                if status:
                    print("Uploaded %d%%." % int(status.progress() * 100))
    print("Upload Complete!")

任何帮助将不胜感激!

我的文件结构: https://gyazo.com/29525bf2660888dc3ab285c8f2a7c6f5

【问题讨论】:

  • 您当前的解决方案有什么问题?您将不得不做一个 file.list 来获取文件夹中所有文件的列表并一次上传一个。

标签: python google-drive-api google-api-python-client


【解决方案1】:

您不能将包含嵌套内容的整个文件夹直接上传到 Google 云端硬盘

但您可以做的一种解决方法是压缩您的文件夹,将压缩文件上传到 Google 云端硬盘并在那里解压缩。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 2012-06-11
    相关资源
    最近更新 更多