寻找使用MediaFileUpload的例子 [英] Looking for example using MediaFileUpload
问题描述
有谁知道我在哪里可以找到完整的示例代码,用于上传本地文件并获取MediaFileUpload的内容?
我真的需要看到HTML表单用于帖子和代码来接受它。我拉我的头发,到目前为止只有部分答案。
谢谢!
我在找出这个问题的同时,试图找出Google API示例中的MediaFileUpload来自何处,最终我发现了它。这是一个更完整的代码示例,我用它来测试Python 2.7的功能。
您需要JSON凭证文件才能使用此代码。这是您从Google app / project / thing获得的凭证文件。
您还需要上传文件,我在此处使用test.html例子。
from oauth2client.service_account import ServiceAccountCredentials $ b $ from apiclient.discovery import build $ b $ from apiclient。 http import MediaFileUpload
#设置凭证对象我认为
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials_from_google_app.json',['https://www.googleapis.com/auth/drive '])
#Now构建我们的api对象,事物
drive_api = build('drive','v3',credentials = creds)
file_name =测试
print正在上传文件+ file_name +...
#我们必须提出一个请求哈希来告诉google API我们给它的
body = {'name':file_name,'mimeType':'application / vnd.google-apps.document'}
#现在创建媒体文件上传对象并告诉它什么文件上传,
#在这种情况下'test.html'
media = MediaFileUpload('test.html',mimetype ='text / html')
#重新做实际的文章,创建一个新文件的上传类型
fiahl = drive_api.files()。create(body = body,media_body = media).execute()
#Because详细程度很好
打印创建文件'%s'id'%s'。 fiahl.get('id'))
A可在
https://developers.google.com/drive/v3/web/mime-types
MediaFileUpload的有效mimetype字符串列表(它们是会尝试将您的文件转换为您在此放置的任何内容):
Does anyone know where I can find complete sample code for uploading a local file and getting contents with MediaFileUpload?
I really need to see both the HTML form used to post and the code to accept it. I'm pulling my hair out and so far only getting partial answers.
Thanks!
I found this question while trying to figure out where the heck "MediaFileUpload" came from in the Google API examples, and I eventually figured it out. Here is a more complete code example that I used to test things with Python 2.7.
You need a JSON credentials file for this code to work. This is the credentials file you get from your Google app / project / thing.
You also need a file to upload, I'm using "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'))
A list of valid Mime Types to use in the "body" hash is available at https://developers.google.com/drive/v3/web/mime-types
A list of valid mimetype strings for the MediaFileUpload (they'll attempt to convert your file to whatever you put here):
这篇关于寻找使用MediaFileUpload的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!