2016/10/24

Google Drive API: Uploading Files And Downloading (Exporting) Google Doc Files Using Python

This is a quick note about the Google Drive API: Uploading & Downloading Files sample at https://youtu.be/-7YH6rdR-tk.

The original Python script can be downloaded from http://wescpy.blogspot.tw/2015/12/google-drive-uploading-downloading.html.

When using the v2 version API, the 'hello.txt' can be uploaded and converted into Google Doc format and stored on my Google Drive. However, the conversion to PDF and download the file to my local disk part isn't working.

Python Script - v2 version API


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from __future__ import print_function
import os

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store, flags) \
            if flags else tools.run(flow, store)
DRIVE = build('drive', 'v2', http=creds.authorize(Http()))

FILES = (
    ('hello.txt', False),
    ('hello.txt', True),
)

for filename, convert in FILES:
    metadata = {'title': filename}
    res = DRIVE.files().insert(convert=convert, body=metadata,
            media_body=filename, fields='mimeType,exportLinks').execute()
    if res:
        print('Uploaded "%s" (%s)' % (filename, res['mimeType']))

if res:
    MIMETYPE = 'application/pdf'
    res, data = DRIVE._http.request(res['exportLinks'][MIMETYPE])
    if data:
        fn = '%s.pdf' % os.path.splitext(filename)[0]
        with open(fn, 'wb') as fh:
            fh.write(data)
        print('Downloaded "%s" (%s)' % (fn, MIMETYPE))



When using the v3 version API, all are fine.

Python Script - v3 version API

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from __future__ import print_function
import os

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store, flags) \
            if flags else tools.run(flow, store)
DRIVE = build('drive', 'v3', http=creds.authorize(Http()))

FILES = (
    ('hello.txt', None),
    ('hello.txt', 'application/vnd.google-apps.document'),
)

for filename, mimeType in FILES:
    metadata = {'name': filename}
    if mimeType:
      metadata['mimeType'] = mimeType
    res = DRIVE.files().create(body=metadata, media_body=filename).execute()
    if res:
        print('Uploaded "%s" (%s)' % (filename, res['mimeType']))

if res:
    MIMETYPE = 'application/pdf'
    data = DRIVE.files().export(fileId=res['id'], mimeType=MIMETYPE).execute()
    if data:
        fn = '%s.pdf' % os.path.splitext(filename)[0]
        with open(fn, 'wb') as fh:
            fh.write(data)
        print('Downloaded "%s" (%s)' % (fn, MIMETYPE))



Below is a list of the MIME Type that could be used at line 25 of the Python Script for v3 version API.

Supported MIME Types - https://developers.google.com/drive/v3/web/mime-types


Overview of the available methods for files.

Google Drive APIs REST v2 - https://developers.google.com/drive/v2/reference/
Google Drive APIs REST v3 - https://developers.google.com/drive/v3/reference/


Code Walk Through - Python Script - v3 version API

For a walk through of line 1 to line 21, please refer to http://wei48221.blogspot.tw/2016/10/notes-on-common-code-section-for.html.

FILES = (
    ('hello.txt', None),
    ('hello.txt', 'application/vnd.google-apps.document'),
)

('hello.txt', None) <- No conversion.
('hello.txt', 'application/vnd.google-apps.document') <- Convert to Google Docs format

for filename, mimeType in FILES:
    metadata = {'name': filename}
    if mimeType:
      metadata['mimeType'] = mimeType
    res = DRIVE.files().create(body=metadata, media_body=filename).execute()
    if res:
        print('Uploaded "%s" (%s)' % (filename, res['mimeType']))

Upload 'hello.txt' twice to my Google Drive. The first upload is saved directly as text file (see below, Type = Text).


The second upload is converted and saved as Google Docs file (see below, Type = Google Docs).


if res:
    MIMETYPE = 'application/pdf'
    data = DRIVE.files().export(fileId=res['id'], mimeType=MIMETYPE).execute()
    if data:
        fn = '%s.pdf' % os.path.splitext(filename)[0]
        with open(fn, 'wb') as fh:
            fh.write(data)
        print('Downloaded "%s" (%s)' % (fn, MIMETYPE))

Export the 'hello.txt' which is of Google Docs type and saved it to the current working folder as a PDF file. Note that export seems to work with Google Doc document only.

--------------------------------------------------------------------------------------------------------------------------

Additional Example - Uploading file including file description

The script below uploads a file named "hello.txt" to Google Drive, sets its mimeType to Google Doc, and sets its description field. After the script is successfully executed, the filename, file description, mimeType, and file id are printed.

Python Script - v3 version API

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from __future__ import print_function
import os

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store, flags) \
            if flags else tools.run(flow, store)
DRIVE = build('drive', 'v3', http=creds.authorize(Http()))

FILES = (('hello.txt', 'A test document', 'application/vnd.google-apps.document'),)

for filename, description, mimeType in FILES:
    metadata = {'name': filename}
    if description:
       metadata['description'] = description
    if mimeType:
      metadata['mimeType'] = mimeType
    res = DRIVE.files().create(body=metadata, media_body=filename).execute()
    if res:
        print('Uploaded "%s" "%s" (%s)' % (filename, description, res['mimeType']))
        print('file id %s' % res['id'])

Result on Google Drive


For info. on how to find file id by filename and download non Google Doc files, please refer to my other post at

References:

Google Drive SDK: Writing your first Drive app in Python

Google Drive APIs REST Download Files

**Google Doc formats and supported export MIME types map to each other as follows**





No comments:

Post a Comment