0
votes

In my Python Appengine 'app' I have been asked to 'attach any file' so I have the following code snippet to 'display' those files...

blobattach = ''
blobmime   = 'None'
if pattachment.blobkey <> None:
    blobattach = get_serving_url(pattachment.blobkey)  # <-- line 104
    blob_info  = blobstore.BlobInfo.get(pattachment.blobkey)
    blobmime   = blob_info.content_type[:5]
    blobname   = blob_info.filename

Using the following HTML

{% if blobmime == 'None' %}
{% else %}
    {% if blobmime == 'image' %}
        <img src="{{ blobattach }}" alt='Attachment'/>
    {% else %}
        <br/>
        <small><a class="fswideb" href="{{blobattach}}" Title="Download to view"><span>Download to view {{ blobname }}</span></a></small>
    {% endif %}
{% endif %}

If the attachment is an image, it is displayed (blobmime=='image'). If not, a link is displayed so the user can download the file and view it however they can.

However, while this works in development, on my laptop (Google App Engine Launcher), I get the following error when trying to 'serve' a .xls file. (No error with .jpg attachments)

File "/base/data/home/apps/s~fs-rentals/20140101.382312463312950329/fmntjobattachmaint.py", line 104, in display
blobattach = get_serving_url(pattachment.blobkey)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/images/__init__.py", line 1794, in get_serving_url
return rpc.get_result()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 613, in get_result
return self.__get_result_hook(self)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/images/__init__.py", line 1892, in get_serving_url_hook
raise _ToImagesError(e, readable_blob_key)

TransformationError

All of the examples use images and I have no problems with them, indeed no problems in development. Any thoughts on what I could have missed?

Many thanks David

As suggested I changed the above to use Google Cloud Storage. I still get exactly the same error. The get_serving_url function errors if the blob is not an image. Is there an equivalent for a file that is not an image?

The sample at https://cloud.google.com/appengine/docs/python/tools/webapp/blobstorehandlers#BlobstoreUploadHandler provides a really good example of what I am trying to do except that I may want to add the person's CV instead of their photo.

Thanks David

1
When you say "this works in development" I think you mean the opposite -- that it works when deployed to appspot but not in the development app server, e.g "on your laptop". I would recommend avoiding blobstore and using the newer, shiny cloud.google.com/appengine/docs/python/googlecloudstorageclient instead (in deployment the old blobstore API's implemented on top of that -- but going with the real GCS should work fine both in deployment and local development, in the latter case GCS being implemented on top of your own local times). - Alex Martelli
I'll have a look and let you know. Thanks for the quick response. - David Freeman
I still see you using the blobstore API in your Q -- where's your code switched to cloud.google.com/appengine/docs/python/googlecloudstorageclient ...? - Alex Martelli

1 Answers

0
votes

Fixed my problem. I was trying to do too much on one 'screen'

If the blob is an image - get_serving_url as above.
If the blob is not an image - display a link to a different page (target='_blank') that does the following (pretty much copied from another SO post):

    if rmntjobattach.mntjobattachid <> 0 \
    and rmntjobattach.blobkey:
        blob_info = blobstore.BlobInfo.get(rmntjobattach.blobkey)
        self.send_blob(blob_info)
    else:
        self.error(404)   

The trick was to display an image blob on the same page and everything else on the new page when the user requested it (clicked the link)

David