Reputation: 394
I want to create a process using GAE by which, given a url, a file is downloaded and stored as a blob in the blobstore. Once this is done I want to pass this blob along as POST data to a second url. However for this second part to work I need to be able to open the blob as a file instance.
I've figured out how to do the first part
from __future__ import with_statement
from google.appengine.api import files
imagefile = urllib2.urlopen('fileurl')
# Create the file
file_name = files.blobstore.create(mime_type=imagefile.headers['Content-Type'])
# Open the file and write to it
with files.open(file_name, 'ab') as f:
f.write(imagefile.read())
# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)
# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)
But I can't figure out how to do the second part. So far I've tried
ffile = files.open(files.blobstore.get_file_name(blob_key), 'r')
from google.appengine.ext import blobstore
ffile = blobstore.BlobReader(blob_key)
from google.appengine.ext import blobstore
ffile = blobstore.BlobInfo.open(blobstore.BlobInfo(blob_key))
All of which gives False
for isinstance(ffile, file)
.
Any help is appreciated.
Upvotes: 2
Views: 1836
Reputation: 11706
To read file_data from the blobstore :
blob_key = ..... # is what you have
file_name = blobstore.BlobInfo.get(blob_key).filename # the name of the file (image) to send
blob_reader = blobstore.BlobReader(blob_key)
file_data = blob_reader.read() # and the file data with the image
But you can also send an url with the blob_key and serve the url. And for images you do not have to serve the images yourself, because you can post a get_serving_url, making use of the Google High Perfomance Image Serving API with dynamic scaling. Serving images this way is also very cheap.
Here is an example of such an url :
Upvotes: 1
Reputation: 469
ffile = blobstore.BlobReader(blob_key)
works. However, the returned object has only a file-like interface; it dosn't extend a file-class. Therefore, the isinstance test wont work. Try something like if ffile and "read" in dir( ffile )
.
Upvotes: 2