如何将Web图像保存到App

如何将Web图像保存到App

本文介绍了如何将Web图像保存到App Engine的blobstore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用作为解决相同问题的模板,但发布时遇到问题。我有这些组件:


  1. HTML 表格,图片网址带有文本框。此帖发布到...

  2. 一个处理程序,它接受发布的URL,对其进行编码并使用 urlfetch 将其重新发布到...

  3. 实际保存的单独的文件上传处理程序

如果我使用文件输入,组件#3可以很好地工作。但我不太明白如何从图片网址获得 urlfetch 所需的内容。我的流程要么超时,要么得到最终处理程序的500响应。

 #1 
class URLMainHandler(RequestHandler):
def get(self):
返回render_response('blob / upload_url.html',
upload_url = url_for('blobstore / upload / url'))
#2 $ b $ class URLUploadHandler(RequestHandler):
def post (self):
导入urllib
#获取发布的图片URL。
data = urllib.urlencode({'file':self.request.form.get('file')})
#通过在文件上传处理程序上调用POST将图像发布到blobstore。
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'是表单中文件上传字段的名称。
upload_files = self.get_uploads('file')
blob_info = upload_files [0]
response = redirect_to('blobstore / serve',resource = blob_info.key())
#清除响应主体。
response.data =''
返回响应

同样,。感谢您的帮助!

解决方案

不使用blobstore api就可以实现同样的功能。我认为你必须得到url并使用urlfetch()。content方法获取内容并将其存储为blob属性。

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

有关将数据存储区中的图像存储并提供为blob的更多参考信息。

你可以在

I've used this question as a template to solve the same problem, but I'm running into issues when posting. I have these components:

  1. HTML form with a textbox for the image URL. This posts to...
  2. A handler that takes the posted URL, encodes it, and uses urlfetch to post it again to...
  3. A separate file upload handler that does the actual saving.

Component #3 works fine by itself if I use a file input. But I don't quite understand how to get urlfetch what it needs from just the image URL. My process either times out or gets a 500 response from the final handler.

# 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

Again, this is the process I'm following. Thanks for your help!

解决方案

You can achieve the same without using blobstore api. I think you have to just get the url and get the content using urlfetch().content method and store it as a blob property.

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

For further reference in storing and serving images from datastore as blob.

You can see this post for more on store-images-in-datastore

这篇关于如何将Web图像保存到App Engine的blobstore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:33