gpt4 book ai didi

python-2.7 - python : How do i download a file from Google drive using api

转载 作者:行者123 更新时间:2023-12-04 19:04:38 25 4
gpt4 key购买 nike

我想使用 google drive api 将 google drive 中的文件下载到我的系统。我该如何实现?

根据 https://developers.google.com/drive/v2/reference/files/get#examples 中给出的示例

def print_file(service, file_id):

"""Print a file's metadata.

Args:
service: Drive API service instance.
file_id: ID of the file to print metadata for.
"""
try:
file = service.files().get(fileId=file_id).execute()

print 'Title: %s' % file['title']
print 'MIME type: %s' % file['mimeType']
except errors.HttpError, error:
print 'An error occurred: %s' % error


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

我能够找到该文件,但是如何将其本地下载到我的机器上?

最佳答案

只需将内容变量写入文件而不是返回内容

fo = open("foo.jpg", "wb")
fo.write(content)
fo.close()

关于python-2.7 - python : How do i download a file from Google drive using api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28308489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com