Google Drive API downloads files - python - files not uploaded

I tried several ways to download files from Google Drive via oauth and the API, however I cannot upload files. I believe that I am authenticated correctly. After running my code, it looks like there was a successful file download (no errors), but the files were not loaded.

This is the code I've tried so far:

def download_file(file_id, mimeType): if "google-apps" in mimeType: return request = drive_service.files().get(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) 

However, this leads to "Download 100%." prints to the console, but the file does not load.

I also tried:

 def download2(download_url): resp, content = drive_service._http.request(download_url) if resp.status == 200: print 'Status: %s' % resp return content else: print 'An error occurred: %s' % resp return None 

This also does not create the downloaded file, but it gives me a 200 message.

Both of them seem to come into contact with the API correctly. Is there another step I must take to actually get the files on my computer?

Edit:

this is the rest of my code:

 import json import webbrowser import httplib2 import io from apiclient.http import MediaIoBaseDownload from apiclient import discovery from oauth2client import client if __name__ == '__main__': flow = client.flow_from_clientsecrets( 'client_secrets.json', scope='https://www.googleapis.com/auth/drive.readonly', redirect_uri='urn:ietf:wg:oauth:2.0:oob') auth_uri = flow.step1_get_authorize_url() webbrowser.open(auth_uri) auth_code = raw_input('Enter the auth code: ') credentials = flow.step2_exchange(auth_code) http_auth = credentials.authorize(httplib2.Http()) drive_service = discovery.build('drive', 'v3', http_auth) #also tried v2 files = drive_service.files().list().execute() for f in files['files']: #call one of the two download methods with the proper arguments 
+5
source share
2 answers

Switching from BytesIO to FileIO allowed to actually download the file. This was the line where I changed my code:

 fh = io.FileIO(filename, 'wb') 

Here is the complete code that allowed me to upload the file:

 def download_file(file_id, mimeType, filename): if "google-apps" in mimeType: # skip google files return request = drive_service.files().get_media(fileId=file_id) fh = io.FileIO(filename, 'wb') downloader = MediaIoBaseDownload(fh, request) done = False while done is False: status, done = downloader.next_chunk() print "Download %d%%." % int(status.progress() * 100) if __name__ == '__main__': flow = client.flow_from_clientsecrets( 'client_secrets.json', scope='https://www.googleapis.com/auth/drive.readonly', redirect_uri='urn:ietf:wg:oauth:2.0:oob') auth_uri = flow.step1_get_authorize_url() webbrowser.open(auth_uri) print auth_uri auth_code = raw_input('Enter the auth code: ') credentials = flow.step2_exchange(auth_code) http_auth = credentials.authorize(httplib2.Http()) drive_service = discovery.build('drive', 'v3', http_auth) files = drive_service.files().list().execute() for f in files['files']: print f['name'] download_file(f['id'], f['mimeType'], f['name']) 
+7
source

The file is loading, but the example provided by google does nothing with the file.

You just need to return the contents of the BytesIO buffer (just adding a return at the end) ...

 def download_file(service, file_id): request = 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)) return fh.getvalue() 
+2
source

Source: https://habr.com/ru/post/1245612/


All Articles