Google云端硬盘api属性错误:“列表”对象没有属性“ uri”

问题描述 投票:0回答:1

我试图理解为什么对download_file()的此函数调用总是引发错误。有人可以帮助我理解为什么会这样吗?

'''

def get_files():
    results = service.files().list(
        pageSize=10, fields="nextPageToken, files(id, name, mimeType)").execute()
    items = results.get('files', [])
    return items

def download_file(id, filename, mimeType):
    file_id = id
    request = get_files()

    fh = io.FileIO(filename, 'wb')
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download {}%.".format(int(status.progress() * 100)))
        print("Done")
    return fh.getvalue()

def download_all_files():
    files = get_files()
    for file in files:
        id = file['id']
        name = file['name']
        the_type = file['mimeType']
        #print(file['id'], file['name'], file['mimeType'])
        download_file(id, name, the_type)

#Calls
download_all_files()

'''

AttributeError:“列表”对象没有属性“ uri”

python google-drive-api
1个回答
1
投票

您遇到的错误:

AttributeError:“列表”对象没有属性“ uri”

是由于在list时将MediaIoBaseDownload对象传递给in the documentation for such object,它指定第二个参数应为googleapiclient.http.HttpRequest

同样,您要调用函数googleapiclient.http.HttpRequest两次,一次迭代每个文件,另一次实际上获取get_files()变量(没有太大意义)。

[在Google文档中,有一个示例实际上是request。在那个例子中,python代码看起来像(python 2代码):

download a file from Google Drive

请参见示例,在这里他们正在调用file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M' request = drive_service.files().get_media(fileId=file_id) fh = io.BytesIO() downloader = MediaIoBaseDownload(fh, request) done = False while done is False: status, done = downloader.next_chunk() print "Download %d%%." % int(status.progress() * 100) 方法,而不是您作为输入传递的文件列表。

因此,我也将更改您的get_media()函数看起来像这样:

get_media()

[仅作为说明,我还要提及download_file实际上是 def download_file(id, filename, mimeType): file_id = id request = drive_service.files().get_media(fileId=id) fh = io.FileIO(filename, 'wb') downloader = MediaIoBaseDownload(fh, request) done = False while done is False: status, done = downloader.next_chunk() print("Download {}%.".format(int(status.progress() * 100))) print("Done") return fh.getvalue() 。所以id

© www.soinside.com 2019 - 2024. All rights reserved.