【问题标题】:Howto download file from Drive API using Python script如何使用 Python 脚本从 Drive API 下载文件
【发布时间】:2013-05-21 07:34:46
【问题描述】:

我正在编写 Python 中的简单脚本,该脚本可以从 Google Apps 域 Drive Service 下载和导出所有文件。我能够使用服务帐户创建驱动器会话,并且从列表查询中获取 JSON 输出。我也根据这篇文章定义了下载功能:

https://developers.google.com/drive/manage-downloads

问题是这个函数返回另一个名为 content 的 JSON 输出,但我不知道如何将文件本地存储在 HDD 上。 我正在研究 CURL 是否可以在 Python 脚本中使用,并发现 urllib/urllib2 应该与 CURL 类似地使用。但是如果我尝试使用 urllib2 来读取远程文件:

remote_file = urllib2.urlopen(download_url).read()

我得到 401 错误未授权

所以看起来 urllib2 正在工作,但不使用存储的凭据。

那么如何使用 urllib/2 创建授权查询?或者从脚本中本地存储文件的正确方法是什么?还有其他一些 python 或 google 特定的库可以帮助我在本地存储文件吗?

提前致谢。

编辑:我正在使用 Google API 客户端库。问题是函数 download_file 正在返回一些 JSON 输出,但我无法将文件保存到本地存储。

我尝试过这样的事情:

def download_file(service, drive_file):
    """Download a file's content.

    Args:
            service: Drive API service instance.
            drive_file: Drive File instance.

    Returns:
            File's content if successful, None otherwise.
    """
    download_url = drive_file.get('downloadUrl')
    if download_url:
            resp, content = service._http.request(download_url)
            if resp.status == 200:
                    print 'Status: %s' % resp
                    #return content
                    title = drive_file.get('title')
                    path = './data/'+title
                    file = open(path, 'wb')
               #    remote_file = urllib2.urlopen(download_url).authorize().read()
                    file.write(content.read())
            else:
                    print 'An error occurred: %s' % resp
                    return None
    else:
            # The file doesn't have any content stored on Drive.
            return None

这会在 HDD 上创建文件,但在尝试读取内容时失败。我不知道如何处理适合写入本地磁盘的内容。

EDIT2:

好的,所以我终于弄清楚了。我的错误是我试图在内容上使用函数 read() 。我只需要使用 file.write(content)

【问题讨论】:

标签: python file download google-drive-api google-apps


【解决方案1】:

您可以从the article 尝试此脚本。并且记得使用Google APIs Client Library for Python

from apiclient import errors
# ...

def download_file(service, drive_file):
    """Download a file's content.

    Args:
    service: Drive API service instance.
    drive_file: Drive File instance.

    Returns:
    File's content if successful, None otherwise.
    """
    download_url = drive_file.get('downloadUrl')
    if download_url:
        resp, content = service._http.request(download_url)
    if resp.status == 200:
        print 'Status: %s' % resp
        return content
    else:
        print 'An error occurred: %s' % resp
        return None
    else:
    # The file doesn't have any content stored on Drive.
    return None

【讨论】:

  • 感谢您的回复,实际上这正是我正在使用的。但问题是这个函数返回的内容只是一个 JSON 输出,但我不知道如何将文件保存到本地存储 (HDD)。
【解决方案2】:

以下代码有助于将文件内容保存在本地文件中。只需在下面的代码中替换路径和文件扩展名。

if download_url:
    resp, content = service._http.request(download_url)
    if resp.status == 200:
        print ('Status: %s' % resp)
        title = file.get('title')
        path = './data/'+title+".csv"
        file1 = open(path, 'wb')
        file1.write(content)
    else:
        print ('An error occurred: %s' % resp)
        return None
else:
    # The file doesn't have any content stored on Drive.
    return None

【讨论】:

    猜你喜欢
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 2021-11-17
    • 2023-02-23
    相关资源
    最近更新 更多