简体   繁体   English

Python App Engine - 用于非图像的Blobstore

[英]Python App Engine - Blobstore for non-images

In my Python Appengine 'app' I have been asked to 'attach any file' so I have the following code snippet to 'display' those files... 在我的Python Appengine'app'中,我被要求'附加任何文件',所以我有以下代码片段来'显示'那些文件......

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 使用以下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'). 如果附件是图像,则显示(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. 但是,虽然这在开发中有效,但在我的笔记本电脑(Google App Engine Launcher)上,尝试“提供”.xls文件时出现以下错误。 (No error with .jpg attachments) (.jpg附件没有错误)

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 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. 如上所述,我将上述内容更改为使用Google云端存储。 I still get exactly the same error. 我仍然得到完全相同的错误。 The get_serving_url function errors if the blob is not an image. 如果blob不是图像,则get_serving_url函数会出错。 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. https://cloud.google.com/appengine/docs/python/tools/webapp/blobstorehandlers#BlobstoreUploadHandler上的示例提供了一个非常好的示例,说明我要尝试做的事情,除了我可能想要添加此人的简历而不是他们的照片。

Thanks David 谢谢大卫

Fixed my problem. 解决了我的问题。 I was trying to do too much on one 'screen' 我试图在一个'屏幕'上做太多

If the blob is an image - get_serving_url as above. 如果blob是一个图像 - get_serving_url如上所述。
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): 如果blob不是图像 - 显示指向不同页面(target ='_ blank')的链接,该链接执行以下操作(几乎从另一个SO帖子复制):

    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) 诀窍是在用户请求时在同一页面和新页面上显示一个图像blob(点击链接)

David 大卫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM