Google Drive API: Uploading & Downloading Files



In a short dozen minutes, Google engineer Wesley Chun guides you through this jam-packed episode featuring an introduction to the new Drive API (v3), an interview with a Google Drive engineer about the API design, and a code walkthrough of real source code you can use today! This time, it’s a command-line script that performs Drive file uploads & downloads, presented first in the original API (v2) followed by a how-to segment on migrating it step-by-step to the new API (v3). In addition, the uploading segment includes the option of converting to Google Apps formats while the download portion covers exporting to alternative formats such as PDF®.

Links from the episode:

* Google Drive new API launch:
* Google Drive API original launch:
* Client Library supported languages:
* Google APIs common code walkthrough:
* Migrating from run() to run_flow():
* Optimizing Drive API calls:
* Google Drive API documentation:
* Code Samples blogpost:
* Launchpad Online developer video series:
* Listing your files in Google Drive:
* File Storage in the Cloud:
* Accessing Google APIs:

Subscribe to the Google Developers channel at –
Subscribe to the brand new Firebase Channel:

Nguồn: https://katieeliselambert.com

Xem thêm bài viết khác: https://katieeliselambert.com/tong-hop/

Article Categories:
Tổng Hợp

All Comments

  • Harry Lukeides : I have a project with youtube through Google to be able to watch or down load Info. I can not download an api file . I am 80 years old Do I have to go to Uni to learn all about computers, or is there a samaritan to help me out, I process a project No. and an id with google ;

    Harry Lukeides June 1, 2020 5:17 am Reply
  • Praise the baby jesus! found this just in time. Thank you for walking us through the code, for the dullards such as I, this is most useful.

    Kapil Chhabria June 1, 2020 5:17 am Reply
  • @google developers, this doesn't work for the shared drives.

    ANAND RAJU June 1, 2020 5:17 am Reply
  • I copied the exact same code but I was getting the following error:
    raise HttpError(resp, content, uri=self.uri)

    googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/upload/drive/v3/files?alt=json&uploadType=multipart returned "Insufficient Permission: Request had insufficient authentication scopes.">

    What should I do to fix it?

    Akshatha Laxmi June 1, 2020 5:17 am Reply
  • hey bro, do you know how to add caption track video (vtt) on google drive with coding ?

    Iwan Krisnawan June 1, 2020 5:17 am Reply
  • Can you tell me whats the content of storage.json ???

    Ghundi June 1, 2020 5:17 am Reply
  • Is it possible to do smth like this in C++ ?

    Legnahar Prime June 1, 2020 5:17 am Reply
  • Why php download code
    Fatal error: Uncaught exception 'Google_Service_Exception' with message '{

    "error": {

    "errors": [

    {

    "domain": "global",

    "reason": "fileNotExportable",

    "message": "Export only supports Google Docs."

    }

    ],

    "code": 403,

    "message": "Export only supports Google Docs."

    }

    }

    ' in C:AppServwwwgooglevendorgoogleapiclientsrcGoogleHttpREST.php:118

    Stack trace:

    #0 C:AppServwwwgooglevendorgoogleapiclientsrcGoogleHttpREST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttpPsr7Response), Object(GuzzleHttpPsr7Request), NULL)

    #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttpClient), Object(GuzzleHttpPsr7Request), NULL)

    #2 C:AppServwwwgooglevendorgoogleapiclientsrcGoogleTaskRunner.php(181): call_user_func_array(Array, Array)

    #3 C:AppServwwwgooglevendorgoogleapiclientsrcGoogleHttpREST.php(58): Google_Task_Runner->run()

    #4 C:AppServwwwgooglevendorgoogleapiclientsrcGoogleClient.php(779): Google_Http_REST::execute(Object(GuzzleHttp in C:AppServwwwgooglevendorgoogleapiclientsrcGoogleHttpREST.php on

    ศราวุธ เหล่าบุรมย์ June 1, 2020 5:17 am Reply
  • How can we upload files in team drive?

    Nishant Srivastav June 1, 2020 5:17 am Reply
  • So, I am supposed to upload videos files greater than several hundred MBs, So, to upload them I am first calling my server side code of C# asp.net and then handing over the file to google drive. Isn't any direct way of uploading files? javascript for asp.net servers? No?

    Jamshaid Kamran June 1, 2020 5:17 am Reply
  • How to download already existing video files in drive? any initial thought?

    prakhar patidar June 1, 2020 5:17 am Reply
  • Whatever it May be it's a better lesson for everyone's to understand the living stength of human beings and a life of others need.

    mogum loyi mogum Loyi June 1, 2020 5:17 am Reply
  • Whatever it May be it's a better lesson for everyone's to understand the living stength of human beings and a life of others need.

    mogum loyi mogum Loyi June 1, 2020 5:17 am Reply
  • WHY DIS CODE NOT ON DOCUMENTATION!?!?!?!?!?!!?!?!?!!?

    Young Nam June 1, 2020 5:17 am Reply
  • I was waiting for him to say python

    Hit June 1, 2020 5:17 am Reply
  • Is there a video to achieve same using Javascript?

    Sharath Chandra June 1, 2020 5:17 am Reply
  • this api is bat shit insane .

    CoreCx June 1, 2020 5:17 am Reply
  • see at 3:16.

    Ja Ke June 1, 2020 5:17 am Reply
  • I run the same code and I had these errors: Can you help me please?

    Traceback (most recent call last):
    File "c:update_costsuploader.py", line 32, in <module>
    res = DRIVE.files().create(body=metadata, media_body=filename).execute()
    File "C:UsersllollAnaconda3libsite-packagesgoogleapiclientdiscovery.py", line 811, in method
    mimetype=media_mime_type)
    File "C:UsersllollAnaconda3libsite-packagesoauth2client_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
    File "C:UsersllollAnaconda3libsite-packagesgoogleapiclienthttp.py", line 550, in _init_
    fd = open(self._filename, 'rb')
    FileNotFoundError: [Errno 2] No such file or directory: 'hello.txt'

    Marina Doria June 1, 2020 5:17 am Reply
  • hi, how get link mp3 in google drive?r, thanks admin

    Thoại Ngô June 1, 2020 5:17 am Reply
  • I cant seem to get this right. It keeps throwing errors for me. Here is the error message
    CODE:
    from _future_ import print_function
    import os

    from apiclient import discovery
    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', scope=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()
    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))

    ERROR:

    /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/raymondm/PycharmProjects/HelloWorld/Demos/v3_download_files.py
    Traceback (most recent call last):
    File "/Users/raymondm/PycharmProjects/HelloWorld/Demos/v3_download_files.py", line 35, in <module>
    res = DRIVE.files().create(body=metadata, media_body=filename).execute()
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/googleapiclient/discovery.py", line 815, in method
    mimetype=media_mime_type)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/googleapiclient/http.py", line 548, in _init_
    fd = open(self._filename, 'rb')
    FileNotFoundError: [Errno 2] No such file or directory: 'hello.txt'

    Raymond Khozanai Mlambo June 1, 2020 5:17 am Reply
  • This was really expiring! Thanks a bunch!

    Geir Skårhaug June 1, 2020 5:17 am Reply
  • Google Drive API is hard to integrate in iOS.
    https://github.com/google/google-api-objectivec-client-for-rest/issues
    lots of issue not solve. Developers need to take a lot of time to solve these issues.

    ABCDE June 1, 2020 5:17 am Reply
  • Hello Mr. Wesley Chun
    My name is Tiska, i'm a college student. Recently I'm learning Google Drive API and I'm interested to learn more, because i want to develop the google drive apps for research in my college. would you mind to answering some question about drive API?
    and may i know your personal email account?
    thank you ^.^

    tiska hardiana June 1, 2020 5:17 am Reply
  • Where do you download the API for linux systems, and can it be done using a simpler language? Like Java?

    Joseph Kreifels II June 1, 2020 5:17 am Reply
  • Great tutorial!! Love it..

    wei48221 June 1, 2020 5:17 am Reply
  • Wesley Chun rocks! m/

    Ownage Juice June 1, 2020 5:17 am Reply
  • in DriveV2, you can export Google Docs using the exportLinks? Now in DriveV3, using the TRY-IT, it only returns the MiMETYPE and no exportlinks. How is this easier for developers?

    Ownage Juice June 1, 2020 5:17 am Reply
  • I wish would have found this hours ago! Thank you!

    Christopher Riewaldt June 1, 2020 5:17 am Reply
  • I have problems about downloading!In Python My code falls in service.files().export  with message export 'only supports google docs'.In my drive i have a xls . Try with pdf mime type (i assume convert xls->pdf), tried with xlsx, xls and a spreadsheet as mimetype too.

    HawksGaze June 1, 2020 5:17 am Reply
  • how can i use in appinventor?

    박재일 June 1, 2020 5:17 am Reply
  • how do this on C#?

    Никита Лебедев June 1, 2020 5:17 am Reply
  • I love Python 3 and Google, too bad Google stays mostly with Python 2.

    Exélixis June 1, 2020 5:17 am Reply
  • nice

    Ryan Sampaio Filho June 1, 2020 5:17 am Reply

Leave a Reply

Your email address will not be published. Required fields are marked *