如何将filepicker.io与谷歌应用程序引擎(blobstore)集成? [英] How do I integrate filepicker.io with google app engine (blobstore)?
问题描述
然后,我如何使用此URL将文件上传到GAE的blobstore?
blobstore只支持通过表单上传文件,所以真正的问题是如何伪造一个包含文件URL的表单POST。
伪造表单帖子是可能的,但使用 Files API
编辑:
b $ b要使用带有urlfetch的File API,可以这样写: $ _
$ b
from __future__ import with_statement
from google.appengine.api从google.appengine.api导入文件
导入urlfetch
$ b $ url url =http://www.facebook.com/somephoto.png
result = urlfetch.fetch(url)
如果result.status_code不是200:
returnsome错误
#创建文件
file_name = files.blobstore.create(mime_type ='application / octet-stream')
#打开文件并写入到它
与files.open(file_name,'a')作为f:
f.write(result.content)
#完成文件。在尝试阅读之前先做这件事。
files.finalize(file_name)
#获取文件的blob键
blob_key = files.blobstore.get_blob_key(file_name)
我没有测试过这个 - 所以如果这不起作用,请告诉我。
另外,我相信您可以将mime_type保留为application / octet-stream,App Engine会尝试猜测正确的类型。如果这不起作用,请尝试将其更改为image / png。或者,对于pdf,mime类型将是'application / pdf'
So I'm trying to use filepicker.io with a GAE application. The filepicker widget returns the URL of the file that a user uploaded.
How do I then use this url to upload the file to GAE's blobstore?
The blobstore only supports "file uploading through a form", so the real question is how to fake a form POST containing the URL of a file.
Faking a form post is possible, but it's much simpler to use the Files API
Edit:
To use the File API with urlfetch you would write something like this:
from __future__ import with_statement
from google.appengine.api import files
from google.appengine.api import urlfetch
url = "http://www.facebook.com/somephoto.png"
result = urlfetch.fetch(url)
if result.status_code not 200:
return "some error"
# Create the file
file_name = files.blobstore.create(mime_type='application/octet-stream')
# Open the file and write to it
with files.open(file_name, 'a') as f:
f.write(result.content)
# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)
# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)
I haven't tested this - so if this doesn't work, let me know.
Also, I believe you can leave the mime_type as 'application/octet-stream' and App Engine will try and guess the proper type. If that doesn't work try changing it to 'image/png'. Or for a pdf the mime type would be 'application/pdf'
这篇关于如何将filepicker.io与谷歌应用程序引擎(blobstore)集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!