如何将filepicker.io与Google App Engine(Blobstore)集成?

4

我正在尝试在GAE应用程序中使用filepicker.io。 filepicker小部件返回用户上传的文件的URL。

那么,我如何使用此URL将文件上传到GAE的Blobstore中?

Blobstore仅支持“通过表单上传文件”,因此真正的问题是如何伪造包含文件URL的表单POST。

3个回答

4
伪造表单提交是可能的,但使用文件API要简单得多。请参考Files API编辑: 要使用文件API和urlfetch,您可以编写以下内容:
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)

我还没有测试过这个方法,如果不行,请告诉我。
同时,我相信您可以将mime_type设置为'application/octet-stream',App Engine会尝试猜测正确的类型。 如果这样行不通,请将其更改为'image/png'。或者对于pdf,mime类型应为'application/pdf'。

哦,我之前也看过这个,但是你要怎么把一个 PDF 文件的内容写入到 blob 对象中呢?它似乎只支持文本。 - kennysong
啊,太棒了,它可以工作!但是文件下载(一旦您在URL上查看它)没有扩展名,所以您不知道它是.pdf文件。有没有一种方法可以附加文件扩展名? - kennysong
@jellyksong,您是否想在从Blobstore提供服务时添加.pdf文件?您能否为此创建一个新问题?请包含您用于提供文件的代码。 - Kyle Finley

1

最受欢迎答案中的文件API已过时,因此我在github上编写了一份要点来解决这个问题。它使用requests和poster将图像的URL发布到您自己的服务器。

https://gist.github.com/jmasonherr/6301914


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接