One more questions:
I tested the download scenario with the service account. (https://developers.google.com/drive/api/v3/manage-downloads)
file_id_download = "1GiewAJc460ccEX-ScDybp0rkLnsD8q5W"
request = service.files().get_media(fileId=file_id_download)
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)
And scripts returns:
python sa_demo.py
Download 100%.
But I can't find the download file in my local. Could you please give me some guidelines on that?
Hi @tianchao-haohan,
The sample creates a in-memory binary stream io.BytesIO(...). If you want to write to a file you can do something like fh = open("myfile", "rb"). See Binary I/O Documentation.
Hi @tianchao-haohan,
The sample creates a in-memory binary stream
io.BytesIO(...). If you want to write to a file you can do something likefh = open("myfile", "rb"). See Binary I/O Documentation.
Update the code to:
fh = open("test_file.csv", "w")
downloader = MediaIoBaseDownload(fh, request)
raise HttpError(resp, content, uri=self._uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1m4xCk89N123456qWeKjOLme6zw98o8G62ghVW8O9nE?alt=media returned \"Only files with binary content can be downloaded. Use Export with Google Docs files.\">
Can you try 'wb' isntead of 'w'?
Yes. I tried both 'w' and 'wb'.
Do I need to change MediaIoBaseDownload to something else? eg. MediaFileBaseDownload ?
Whoops sorry, I was reading the documentation incorrectly. I think this StackOverflow question answers your question well. You can either read from fh and write to a file, or open a io.FileIO object.
Change:
request = service.files().get_media(fileId=file_id_download)
To:
request = service.files().export_media(fileId=file_id_download, mimeType='text/csv')
Works for me now.
Appreciate your help!
I'm closing off this issue as it is resolved.
Most helpful comment
Change:
request = service.files().get_media(fileId=file_id_download)To:
request = service.files().export_media(fileId=file_id_download, mimeType='text/csv')Works for me now.
Appreciate your help!