1

我想从另一个站点获取图像(~250Kb)并将它们保存到blobstore。我想使用 Blobstore,因为它有配额(5Gb 免费)与数据存储(1Gb 免费)。我该怎么做?

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

4

1 回答 1

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)                                 
于 2012-10-04T19:05:02.743 回答