简体   繁体   English

如何将base64图像存储为GAE数据存储区中的文件

[英]How to store base64 image as a file in GAE datastore

I have an image encoded with base 64 that I want to store in my datastore model. 我有一个用base 64编码的图像,我希望将其存储在我的数据存储模型中。

class Surveys(db.Model):
    name = db.StringProperty(required = True)
    text = db.TextProperty(required = True)
    image = db.BlobProperty()
    created = db.DateTimeProperty(auto_now_add = True)

How do I turn the base64 string back into a file that I can put into the database? 如何将base64字符串转换回我可以放入数据库的文件? Below is how I would do it for a normal file. 以下是我如何为普通文件执行此操作。

name = 'test'
text = 'test'
image = self.request.get('img')
s = Surveys(name = name, text = text)
s.image = db.Blob(image)
s.put()

Are you looking for a way to decode base64 data? 您在寻找解码base64数据的方法吗?

You might wish to take a look at the various base64 utilities available with Python. 您可能希望查看Python提供的各种base64实用程序 For example, base64.b64decode : 例如, base64.b64decode

import base64

binary_data = base64.b64decode(base64_encoded_string)

Assuming the JPEG file was properly encoded as base64, this will "reverse" the operation -- returning a string of bytes identical to the content of the original file. 假设JPEG文件被正确编码为base64,这将“反转”操作 - 返回与原始文件内容相同的字节串。 All file "meta informations" are lost in the process: you only get back the content of the file. 所有文件“元信息”在此过程中都会丢失:您只能获取文件的内容。 Not its original name, permissions, and so on. 不是其原始名称,权限等。

You can either store the base64 string to datastore directly, then decode it during runtime when you need to send the JPEG bytes. 您可以将base64字符串直接存储到数据存储中,然后在运行时在需要发送JPEG字节时对其进行解码。

Or do it the other way round... I'd prefer decode the base64 first before storing to datastore as it's more byte efficient and you only need to decode once. 或者反过来做...我更喜欢在存储到数据存储区之前首先解码base64,因为它的字节效率更高,你只需要解码一次。

And you don't need the concept of "file" here... you just store the image as bytes, when you need to send it out as JPEG to browser, you just create the proper http headers (eg Content-Type:image/jpeg) and echo/write the bytes in the http body. 并且您在这里不需要“文件”的概念……您只需将图像存储为字节,当您需要将其以JPEG格式发送到浏览器时,只需创建适当的http标头即可(例如Content-Type:image / jpeg)并回显/写入http体中的字节。

To add to everything else (which is all good advice): Consider using ndb (new database) rather than db for the model. 要添加到其他所有内容(这都是很好的建议):考虑使用ndb(新数据库)而不是db作为模型。 If you do a get_by_id() for single image retrieval, ndb will handle the memecache for you. 如果对单个图像检索执行get_by_id(),则ndb将为您处理memecache。 This will really speed up your latency for frequently accessed images. 这将真正加快您经常访问的图像的延迟。 Also if it is a frequently accessed image, add a cache control statement to your response header in addition to the type. 另外,如果它是一个经常访问的图像,则除了类型之外,还要在响应标头中添加一个缓存控制语句。 HTH. HTH。 -stevep -stevep

self.response.headers['Content-Type'] = 'image/jpeg'
self.response.headers['Cache-Control']  = 'public, max-age=315360000'

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

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