如何获取图像并保存到blobstore? [英] How to fetch image and save to blobstore?

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

问题描述

我想从其他网站获取图片(〜250Kb)并将其保存到 Blob存储。由于配额(5Gb免费)vs数据存储(1Gb免费),我想使用Blobstore。我该怎么做?



GAE文档说我应该创建上传表单来使用blobstore,而我并不需要它。 解决方案

我认为这段代码会起作用:

  from __future__ import with_statement#first line of your代码
....
from google.appengine.api import urlfetch
import mimetypes $ b $ from google.appengine.api导入文件
.....
image_name ='your_image.png'
response = urlfetch.fetch('http://....../'+ image_name)#response.status_code == 200
(mimetype,_ )= mimetypes.guess_type(image_name)
file_name = files.blobstore.create(mime_type = mimetype,_blobinfo_uploaded_filename = image_name))
with files.open(file_name,'a')as f:
f.write(response.content)
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)


I would like to fetch images (~250Kb) from another site and save them to blobstore. I would like to use Blobstore due to its quota (5Gb free) vs datastore (1Gb free). How can I do it?

GAE docs say that I should create upload form to use blobstore, which I don't need.

解决方案

I think this code will work:

from __future__ import with_statement                     # first line of your code     
....
from google.appengine.api import urlfetch
import mimetypes
from google.appengine.api import files
.....
image_name = 'your_image.png'
response = urlfetch.fetch('http://....../' + image_name)  # response.status_code == 200 
(mimetype, _) = mimetypes.guess_type(image_name) 
file_name = files.blobstore.create(mime_type= mimetype, _blobinfo_uploaded_filename= image_name))
with files.open(file_name, 'a') as f:                                           
    f.write(response.content)
files.finalize(file_name)                       
blob_key = files.blobstore.get_blob_key(file_name)                                 

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆