Damien
Damien

Reputation: 439

Erratic problem with App Engine when writing files directly to the blobstore

I'm using App Engine with Python. In order to store the images of my users, I write them directly to the blobstore as indicated in Google documentation.

My code is below:

# Image insertion in the blobstore
file_name = files.blobstore.create(mime_type='image/jpeg')
with files.open(file_name, 'a') as f:
    f.write(self.imageContent)
files.finalize(file_name)
self.blobKey = files.blobstore.get_blob_key(file_name)
logging.info("Blobkey: "+str(self.blobKey))

The problem is erratic. I don't change anything and since yesterday sometimes it works sometimes it doesn't work. Why? As I print the blobkey (last line of my code), I can see whether the image has been saved into the blobstore or not.

When it works, I have the following line displayed:

Blobkey: AMIfv94p1cFdqkZa3AhZUF2Tf76szVEwpGgwOpN...

When it doesn't work, I have this in my logs:

Blobkey: None

Last detail: images (self.imageContent) are preprocessed and converted into .JPEG before each write.

EDIT:
Everytime, the images are stored in the blobstore (I can see them in the blobviewer in the Administration console). So that's the get_blob_key function which is malfunctioning...

I would like to know what should I do in such a situation? Am I doing something wrong that makes App Engine behavior erratic. How can I solve this out?

Upvotes: 3

Views: 387

Answers (1)

Damien
Damien

Reputation: 439

I finally managed to solve this problem by making the thread sleep during intervals of 50ms

This is the code I added:

# Sometimes blobKey is None
self.blobKey = files.blobstore.get_blob_key(file_name)

# We have to make it wait til it works!
for i in range(1,3):
    if(self.blobKey):
         break
    else:
        logging.info("blobKey is still None")
        time.sleep(0.05)
        self.blobKey = files.blobstore.get_blob_key(file_name)

logging.info("Blobkey: "+str(self.blobKey))

Of course, you have to import the time module to make it work.

import time

I pretty much did the same as the person in the Issue 4872 that systempuntoout mentioned.

Thanks. Please feel free to add any suggestion.

Upvotes: 3

Related Questions