Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto download file from Drive API using Python script

I'm working on simple script in Python which could download and export all files from Google Apps domain Drive Service. I was able to create Drive session using service account and I'm getting JSON output from list query. I also defined download function according to this article:

https://developers.google.com/drive/manage-downloads

The problem is that this function return another JSON output called content, but I can't figure out how to store the file locally on HDD. I was looking into CURL if it can be used inside Python script and found urllib/urllib2 which should be used similarly as CURL. But if I try to use urllib2 to read the remote file by:

remote_file = urllib2.urlopen(download_url).read()

I get 401 Error Unathorized.

So it looks like the urllib2 is working but doesn't use the stored credentials.

So How can I create authorized query using urllib/2? Or what is the right way to store the file locally from within the script? Is there some other other python or google specific library which can help me to store files locally?

Thanks in advance.

EDIT: I am using Google APIs Client Library. The problem is that the function download_file is returning some JSON output but i'm not able to save the file to local storage.

I tried something like this:

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
                    title = drive_file.get('title')
                    path = './data/'+title
                    file = open(path, 'wb')
               #    remote_file = urllib2.urlopen(download_url).authorize().read()
                    file.write(content.read())
            else:
                    print 'An error occurred: %s' % resp
                    return None
    else:
            # The file doesn't have any content stored on Drive.
            return None

This creates the file on HDD, but it fails when tries to read the content. I don't know how to process the content to be suitable to writing to local disk.

EDIT2:

Ok, so I finally figure that out. My mistake was that i tried to use function read() on the content. I just have to use file.write(content).

like image 528
Beneato Avatar asked May 21 '13 07:05

Beneato


1 Answers

You may try this script from the article. And remember to use the Google APIs Client Library for Python.

from apiclient import errors
# ...

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
        return None
    else:
    # The file doesn't have any content stored on Drive.
    return None
like image 82
Dinever Avatar answered Sep 20 '22 23:09

Dinever