Python 使用 Django 模型在 DB 中存储图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18747730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing Images In DB Using Django Models
提问by Mayank Jain
I am using Django for creating one Web service and I want that web service to return Images. I am deciding the basic architecture of my Web service. What I came up to conclusion after stumbling on google is:
我正在使用 Django 创建一个 Web 服务,并且我希望该 Web 服务返回图像。我正在决定我的 Web 服务的基本架构。我在谷歌上绊倒后得出的结论是:
- I should store Images in DB after encoding them to Base64 format.
- Transferring the Images would be easy when directly Bases64 decoded string is transmitted.
- 将图像编码为 Base64 格式后,我应该将图像存储在数据库中。
- 当直接传输 Bases64 解码字符串时,传输图像会很容易。
But I have one issue how can I store bases64 encoded string in DB using Django models? Also, if you see any flaw in my basic architecture please guide me.
但是我有一个问题,如何使用 Django 模型在 DB 中存储 base64 编码的字符串?另外,如果您发现我的基本架构有任何缺陷,请指导我。
I am new to Web services and Django
我是 Web 服务和 Django 的新手
Thanks!!
谢谢!!
回答by Mingyu
You can store image in bytea field.
您可以将图像存储在bytea 字段中。
The bytea data typeallows storage of binary strings. Postgres Documentation Link
所述BYTEA数据类型允许二进制字符串的存储。Postgres 文档链接
The earlier version of Django does not support bytea field, so I have been using this library called djorm-ext-pgbytea:
Django 的早期版本不支持 bytea 字段,所以我一直在使用这个名为djorm-ext-pgbytea 的库:
回答by Cartucho
Some people states that using DB for storing images is not a good idea but that's no true. My advice is to use Django with AppEngine Blobstore Servicein this way:
有些人表示使用 DB 存储图像不是一个好主意,但事实并非如此。我的建议是以这种方式将 Django 与AppEngine Blobstore 服务结合使用:
First, create a Django Custom Storage(or pick one from someone else like this one):
首先,创建一个Django 自定义存储(或者像这样从其他人那里选择一个):
from django.core.files.storage import Storage
class AppEngineBlobStorage(Storage):
def exists(self, name):
...
def size(self, name):
...
def url(self, name):
...
def delete(self, name):
...
def listdir(self, path):
raise NotImplementedError()
This custom storage can receive Django images, convert them to Base64 strings and send them to your AppEngine Blobstore Service application (via xmlrpc for example).
此自定义存储可以接收 Django 图像,将它们转换为 Base64 字符串并将它们发送到您的 AppEngine Blobstore 服务应用程序(例如,通过 xmlrpc)。
Then, create a Django Image model:
然后,创建一个 Django Image 模型:
from django.db import models
from django.db.models.fields.files import ImageField
from .storage import AppEngineBlobStorage
class Photo(models.Model):
caption = models.CharField(max_length=64, blank=True)
blob = ImageField(
upload_to='BlobStorage',
storage=AppEngineBlobStorage(),
max_length=255,
blank=False,
)
serving_url = models.URLField()
...
Then, you have to create an AppEngine application for receiving Django requests for storing images, transform Base64 strings to raw and store them in a Blob. For example:
然后,您必须创建一个 AppEngine 应用程序来接收用于存储图像的 Django 请求,将 Base64 字符串转换为原始字符串并将它们存储在 Blob 中。例如:
# coding=utf-8
from __future__ import with_statement
import webapp2
from base64 import b64decode
from StringIO import StringIO
from xmlrpcserver import XmlRpcServer
from google.appengine.api import files
from google.appengine.api import images
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
class ImageRpcServer(object):
def upload(self, meta, photo, mime_type):
data = b64decode(photo)
file_name = files.blobstore.create(mime_type=mime_type)
with files.open(file_name, 'a') as f:
f.write(data)
files.finalize(file_name)
key = files.blobstore.get_blob_key(file_name)
return str(key)
def serving_url(self, meta, key):
return images.get_serving_url(blobstore.BlobKey(key))
...
Serving a blob
服务一个 blob
The key is the function get_serving_url. From Google docs:
关键是函数get_serving_url。来自谷歌文档:
If you are serving images, a more efficient and potentially less-expensive method is to use get_serving_url using the App Engine Images API rather than send_blob. The get_serving_url function lets you serve the image directly, without having to go through your App Engine instances.
如果您正在提供图像,一种更有效且可能更便宜的方法是使用 App Engine 图像 API 而非 send_blob 使用 get_serving_url。get_serving_url 函数可让您直接提供图像,而无需通过 App Engine 实例。
Finally, by serving images with AppEngine, you can use the awesome feature of resize and crop images (check the documentation of get_serving_url
function), for example:
最后,通过使用 AppEngine 提供图像,您可以使用调整大小和裁剪图像的强大功能(查看get_serving_url
函数文档),例如:
// Resize the image to 32 pixels (aspect-ratio preserved) http://your_app_id.appspot.com/randomStringImageId=s32
// 将图像调整为 32 像素(保留纵横比) http://your_app_id.appspot.com/randomStringImageId=s32
Hope it helps. Good luck!
希望能帮助到你。祝你好运!