如何使用 Flask 模型在 postgres 中存储图像
how to store image in postgres using Flask model
我正在尝试使用烧瓶模型存储图像。我不知道如何将图像存储在 postgres 中,所以我将图像编码为 base64
并且我试图将生成的文本存储在 postgres 中。它正在工作,但有没有推荐的方法使用烧瓶模型
在 postgres 中存储编码文本或图像
class User_tbl(db.Model):
id = db.Column(db.Integer,primary_key=True)
mobile=db.Column(db.String(13),unique=True)
country=db.Column(db.String(30))
image=db.Column(db.String(256))
def __init__(self,mobile,country,image):
self.mobile=mobile
self.country=country
self.image = image
我知道 PostgreSQL 中用于处理 base64 数据的编码和解码函数,请参阅:
https://www.postgresql.org/docs/current/static/functions-string.html
(encode/decode)
谢谢,
避免所有这些编码和解码并简单地将其保存为二进制 blob 会容易得多。在这种情况下,使用 sqlalchemy.dialects.postgresql.BYTEA
列。
通过 flask 在 postgres 中存储图像的 recomended 方法是将图像存储在静态文件夹中(存储 Javascript 和 CSS 文件的位置)并通过网络服务器,即 nginx。它将能够比 flask.You 更有效地做到这一点应该只将图像的路径存储在 postgres 上,然后将实际图像存储在文件系统上。
我知道现在回答这个问题可能为时已晚,但在这几天我试图解决类似的问题,none 提出的解决方案似乎阐明了主要问题。
当然,任何最佳实践都取决于您的需求。然而,一般而言,您会发现将文件嵌入数据库并不是一个好的做法。这要看情况。
阅读“Storing Binary files in the Database" produced by postgresql wiki, I discovered that there are some circumtances in which this practice is instead higly recommended, for instance when the files must be ACID。
在这些情况下,至少在 Postgres 中,bytea 数据类型优于文本或 BLOB 二进制,有时以服务器的一些更高内存要求为代价。
在这种情况下:
1) 你不需要特殊的 sqlalchemy 方言。 LargeBinary 数据类型就足够了,因为它将被翻译为 "large and/or unlengthed binary type for the target platform"。
2) 在 PostgreSQL 中您不需要任何 encode/decode 函数,当然在这种特定情况下。
3) 正如我之前所说,将文件保存到文件系统中并不总是一个好的策略。在任何情况下都不要使用带有 base64 编码的文本数据类型。您的数据或多或少会膨胀 33%,从而导致巨大的存储影响,而 bytea 没有同样的缺点
因此,我建议对您的模型进行以下更改:
class User_tbl(db.Model):
id = db.Column(db.Integer,primary_key=True)
mobile=db.Column(db.String(13),unique=True)
country=db.Column(db.String(30))
image=db.Column(db.LargeBinary)
然后您只需将 FileStorage 参数作为二进制文件传递即可将文件保存到 Postgres 中:
image = request.files['fileimg'].read()
我正在尝试使用烧瓶模型存储图像。我不知道如何将图像存储在 postgres 中,所以我将图像编码为 base64
并且我试图将生成的文本存储在 postgres 中。它正在工作,但有没有推荐的方法使用烧瓶模型
class User_tbl(db.Model):
id = db.Column(db.Integer,primary_key=True)
mobile=db.Column(db.String(13),unique=True)
country=db.Column(db.String(30))
image=db.Column(db.String(256))
def __init__(self,mobile,country,image):
self.mobile=mobile
self.country=country
self.image = image
我知道 PostgreSQL 中用于处理 base64 数据的编码和解码函数,请参阅:
https://www.postgresql.org/docs/current/static/functions-string.html
(encode/decode)
谢谢,
避免所有这些编码和解码并简单地将其保存为二进制 blob 会容易得多。在这种情况下,使用 sqlalchemy.dialects.postgresql.BYTEA
列。
通过 flask 在 postgres 中存储图像的 recomended 方法是将图像存储在静态文件夹中(存储 Javascript 和 CSS 文件的位置)并通过网络服务器,即 nginx。它将能够比 flask.You 更有效地做到这一点应该只将图像的路径存储在 postgres 上,然后将实际图像存储在文件系统上。
我知道现在回答这个问题可能为时已晚,但在这几天我试图解决类似的问题,none 提出的解决方案似乎阐明了主要问题。 当然,任何最佳实践都取决于您的需求。然而,一般而言,您会发现将文件嵌入数据库并不是一个好的做法。这要看情况。 阅读“Storing Binary files in the Database" produced by postgresql wiki, I discovered that there are some circumtances in which this practice is instead higly recommended, for instance when the files must be ACID。 在这些情况下,至少在 Postgres 中,bytea 数据类型优于文本或 BLOB 二进制,有时以服务器的一些更高内存要求为代价。
在这种情况下: 1) 你不需要特殊的 sqlalchemy 方言。 LargeBinary 数据类型就足够了,因为它将被翻译为 "large and/or unlengthed binary type for the target platform"。 2) 在 PostgreSQL 中您不需要任何 encode/decode 函数,当然在这种特定情况下。 3) 正如我之前所说,将文件保存到文件系统中并不总是一个好的策略。在任何情况下都不要使用带有 base64 编码的文本数据类型。您的数据或多或少会膨胀 33%,从而导致巨大的存储影响,而 bytea 没有同样的缺点
因此,我建议对您的模型进行以下更改:
class User_tbl(db.Model):
id = db.Column(db.Integer,primary_key=True)
mobile=db.Column(db.String(13),unique=True)
country=db.Column(db.String(30))
image=db.Column(db.LargeBinary)
然后您只需将 FileStorage 参数作为二进制文件传递即可将文件保存到 Postgres 中:
image = request.files['fileimg'].read()