1

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

  1. 带有图像 URL 文本框的HTML表单。这个帖子到...
  2. 一个处理程序,它获取已发布的 URL,对其进行编码,并用于urlfetch再次将其发布到...
  3. 执行实际保存的单独文件上传处理程序。

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

同样,这是我正在遵循的过程。谢谢你的帮助!

4

2 回答 2

3

您可以在不使用 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的更多信息

于 2011-03-21T06:09:48.840 回答
3

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

A future release of the SDK will include the ability to programmatically store blobs in the blobstore, which avoids the need for this nasty hack.

If your images are less than 1MB in size, though, a much simpler solution is to store the image directly in the datastore, as Abdul suggests in his answer.

于 2011-03-22T04:06:17.250 回答