Skip to main content
 首页 » 编程设计

python之如何将 Web 图像保存到 App Engine 的 blobstore

2025年05月04日30fff_TT

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

  1. HTML 表单,带有图像 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。感谢您的帮助!

请您参考如下方法:

您可以在不使用 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 上查看此帖子以获取更多信息