【问题标题】:How do I save web images to App Engine's blobstore?如何将网络图像保存到 App Engine 的 blobstore?
【发布时间】:2011-07-19 05:06:24
【问题描述】:

我使用this question 作为模板来解决同样的问题,但我在发布时遇到了问题。我有这些组件:

  1. HTML form 带有用于图像 URL 的文本框。此帖发给...
  2. 一个处理程序,获取发布的 URL,对其进行编码,然后使用urlfetch 再次将其发布到...
  3. 一个单独的文件上传处理程序进行实际保存。

如果我使用文件输入,组件 #3 本身就可以正常工作。但我不太明白如何仅从图像 URL 中获取 urlfetch 所需的内容。我的进程要么超时,要么从最终处理程序获得 500 响应。

# 1
class URLMainHandler(RequestHandler):
    def get(self):
        return render_response('blob/upload_url.html', 
                               upload_url=url_for('blobstore/upload/url'))
# 2        
class URLUploadHandler(RequestHandler):
    def post(self):
        import urllib
        # Get the posted image URL. 
        data = urllib.urlencode({'file': self.request.form.get('file')})
        # Post image to blobstore by calling POST on the file upload handler. 
        result = urlfetch.fetch(url=blobstore.create_upload_url(url_for('blobstore/upload')),
                                payload=data, 
                                method=urlfetch.POST)

        return self.redirect(url_for('blobstore/url'), result.status_code)

# 3
class UploadHandler(RequestHandler, BlobstoreUploadMixin):
    def post(self):
        # 'file' is the name of the file upload field in the form.
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        response = redirect_to('blobstore/serve', resource=blob_info.key())
        # Clear the response body.
        response.data = ''
        return response

再次,this is the process I'm following。感谢您的帮助!

【问题讨论】:

  • #2 处理程序只有 30 秒的时间来获取文件并将其上传到 blobstore 处理程序。

标签: python image google-app-engine blobstore tipfy


【解决方案1】:

您可以在不使用 blobstore api 的情况下实现相同的目的。我认为您必须只获取 url 并使用 urlfetch().content 方法获取内容并将其存储为 blob 属性。

url = "imageurl"
result = urlfetch.fetch(url)
if result.status_code == 200:
   prof.avatar = db.Blob(result.content)

有关将数据存储中的图像作为 blob 存储和提供的进一步参考。

您可以在store-images-in-datastore 上查看此帖子以了解更多信息

【讨论】:

【解决方案2】:

您不能只将图像作为 blobstore HTTP 请求的负载并期望它了解如何处理它。 blobstore 需要 application/multipart-form-data 类型的消息,这是您上传到 blobstore 时浏览器提供的消息。有一个库可以为您执行此操作here

SDK 的未来版本将包括以编程方式将 blob 存储在 blobstore 中的功能,从而避免了这种讨厌的 hack。

但是,如果您的图像大小小于 1MB,则更简单的解决方案是将图像直接存储在数据存储中,正如 Abdul 在他的回答中所建议的那样。

【讨论】:

  • 谢谢,尼克。是否可以混合和匹配 Blobstore 以在上传图像后提供图像,这样您就不必为每个模型制作自定义处理程序?一个例子在这里stackoverflow.com/questions/4763715/…,但它对我不起作用。毕竟,BlobProperty 没有键。 Blobstore 中是否有可以弥补差距的功能?
  • @Wraith 不,您只能使用 blobstore 来提供存储在 blobstore 中的图像。不过,您可以创建一个模型来存储图像,并使用 ReferenceProperty 来引用来自其他模型的图像。
  • 使用了建议的hack。它给出了这个国王的错误...... RequestTooLargeError: The request to API call file.Append() is too large.感谢您的帖子,我们希望尽快提供相同的 api 访问......
  • @sandeep 您需要将写入分成多个块 - 单个 API 调用不能超过 1MB。
猜你喜欢
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多