Blobstore in production does not get BlobKey (while in dev_appserver it does)

Viewed 975

I've got a process that fetches an image from a given URL and saves it to Google Cloud Storage and retrieves a serving url. Now, my understanding (from the documentation I've read) is that I must also create a blob in the process that references the GCS file. If my understanding is incorrect, please let me know.

The handler that fetches the image and tries to save it is:

img_request = urlfetch.fetch(img_url)
    if img_request.status_code == 200:
        img_title               = hashlib.sha1(img_url).hexdigest()
        img_content             = img_request.content
        img_type                = img_request.headers['content-type']
        cloud_storage_filename  = '/images/%s/%s' % (self.source_name, img_title)

        blobstore_filename      = '/gs%s' % cloud_storage_filename
        blobstore_key           = blobstore.create_gs_key(blobstore_filename)

        cloud_storage_file      = gcs.open(filename=cloud_storage_filename, mode='w', content_type=img_type)
        cloud_storage_file.write(img_content)
        cloud_storage_file.close()

        # If I print blobstore_key at this stage, I am getting a result here.
        blobstore_key           = blobstore.get(blobstore_key).key()
        blobstore_serving_url   = images.get_serving_url(blobstore_key)

        # Structured Property to be part of a Datastore Model
        img_model = data_model.Image(
            s_source_url    = img_url,
            k_blob_key      = blobstore_key,
            s_serving_url   = blobstore_serving_url
        )
        return img_model

This works fine on the dev_appserver, however, in production it raises the following error:

   blobstore_key           = blobstore.get(blobstore_key).key()
   AttributeError: 'NoneType' object has no attribute 'key'

Why is this error being raised in Production and not on the dev_appserver? As if the key is not being generated in the first place...

As I have commented in the code, when I print the blobstore_key before calling blobstore.get(blobstore_key).key(), it does print out a key, so it's working fin until there. The problem is, presumably, when I call blobstore.get() on the key, it gets None... why???

Or am I doing something completely wrong? Thanks!

4 Answers
Related