Reputation: 51
I am trying to understand why this function call to download_file() keeps throwing an error. Can someone help me understand why this is the case?
'''
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: 'list' object has no attribute 'uri'
Upvotes: 0
Views: 1520
Reputation: 2676
The error you are having:
AttributeError: 'list' object has no attribute 'uri'
Is caused because you are passing a list
object to the MediaIoBaseDownload
when in the documentation for such object it specifies that the the second parameter should be a googleapiclient.http.HttpRequest
.
Also you are calling the function get_files()
two times, one to iterate to every file and the other to actually get the request
variable (which doesn't make much sense).
In the google documentation there is an example to actually download a file from Google Drive. In that example the python code looks like (python 2 code):
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)
See that here in the example they are invoking the get_media()
method instead of the list of files that you are passing as input.
So based in that I would change your download_file
function too look something like this:
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()
Just as a note I would like to also mention that id
is actually a built in function of python. So you shouldn't be using it as variable name
Upvotes: 1