1

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.

LA_
  • 19,823
  • 58
  • 172
  • 308

1 Answers1

3

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)                                 
voscausa
  • 11,253
  • 2
  • 39
  • 67
  • This might break for images bigger than 1MB if I remember it right.. (the writing part) – Lipis Oct 06 '12 at 17:46
  • If there is a limit, you can write in chuncks. See this question: http://stackoverflow.com/questions/5522804/1mb-quota-limit-for-a-blobstore-object-in-google-app-engine – voscausa Oct 06 '12 at 17:54
  • Yep..! I'm not in front of a computer.. but it would be nice to have that as an example here in Python as well.. just to complete the answer for writing bigger stuff :) – Lipis Oct 06 '12 at 17:56