如何获取图像并保存到blobstore?

时间:2012-10-04 18:03:25

标签: python google-app-engine app-engine-ndb

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

GAE文档说我应该创建上传表单以使用blobstore,这是我不需要的。

1 个答案:

答案 0 :(得分:3)

我认为这段代码可行:

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)