Look for an example using MediaFileUpload

Does anyone know where I can find a complete sample code to download a local file and retrieve content using MediaFileUpload?

I really need to see both the HTML form used for publishing and the code to accept it. I am pulling my hair out and so far I get only partial answers.

Thanks!

+7
source share
2 answers

I found this question, trying to figure out where the "MediaFileUpload" came from from the Google API examples, and I finally figured it out. Here is a more complete sample code that I used to test using Python 2.7.

You need a JSON credential file for this code to work. This is the credential file that you get from your Google app / project / stuff.

You also need a file to download, I use "test.html" here in the example.

from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build from apiclient.http import MediaFileUpload #Set up a credentials object I think creds = ServiceAccountCredentials.from_json_keyfile_name('credentials_from_google_app.json', ['https://www.googleapis.com/auth/drive']) #Now build our api object, thing drive_api = build('drive', 'v3', credentials=creds) file_name = "test" print "Uploading file " + file_name + "..." #We have to make a request hash to tell the google API what we're giving it body = {'name': file_name, 'mimeType': 'application/vnd.google-apps.document'} #Now create the media file upload object and tell it what file to upload, #in this case 'test.html' media = MediaFileUpload('test.html', mimetype = 'text/html') #Now we're doing the actual post, creating a new file of the uploaded type fiahl = drive_api.files().create(body=body, media_body=media).execute() #Because verbosity is nice print "Created file '%s' id '%s'." % (fiahl.get('name'), fiahl.get('id')) 

The list of valid Mime types for use in the "body" hash is available at https://developers.google.com/drive/v3/web/mime-types

The list of valid mimetype lines for MediaFileUpload (they will try to convert your file to what you put here):

https://developers.google.com/drive/v3/web/integrate-open#open_files_using_the_open_with_contextual_menu

+7
source

You will not need to host JSON yourself, the client library handles this for you.

We provide complete code examples that can be found here: https://code.google.com/p/google-drive-sdk-samples/source/browse/#hg%2Fpython

You can also check out the file.insert help documentation that contains a Python sample: https://developers.google.com/drive/v2/reference/files/insert

If this does not answer what you want, perhaps you could explain in more detail what you want to achieve and your architecture at present.

0
source

All Articles