詳解如何使用beego orm在postgres中存儲(chǔ)圖片
Postgres如何存儲(chǔ)文件
postgres提供了兩種不同的方式存儲(chǔ)二進(jìn)制,要么是使用bytea類(lèi)型直接存儲(chǔ)二進(jìn)制,要么就是使用postgres的LargeObject功能;決定使用哪中方式更加適合你,就需要了解這兩種存儲(chǔ)方式有哪些限制
bytea類(lèi)型
bytea類(lèi)型在單列中雖然可以支持1GB的容量,但是還是不建議使用bytea去儲(chǔ)存比較大的對(duì)象,因?yàn)樗鼤?huì)占用大量的內(nèi)存
下面通過(guò)一個(gè)例子來(lái)說(shuō)明,假如現(xiàn)在要在一個(gè)表中存儲(chǔ)圖片名和該圖片的數(shù)據(jù),創(chuàng)建表如下:
CREATE TABLE images (imgname text, img bytea);
在表中插入一張圖片:
File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setBinaryStream(2, fis, file.length());
ps.executeUpdate();
ps.close();
fis.close();
上面的setBinaryStream就會(huì)將圖片內(nèi)容設(shè)置到img字段上面,也可以使用setBytes()直接設(shè)置圖片的內(nèi)容
接下來(lái),從表中取出圖片,代碼如下:
PreparedStatement ps = con.prepareStatement("SELECT img FROM images WHERE imgname = ?");
ps.setString(1, "myimage.gif");
ResultSet rs = ps.executeQuery();
if (rs != null) {
while (rs.next()) {
byte[] imgBytes = rs.getBytes(1);
// use the data in some way here
}
rs.close();
}
ps.close();
Large Object
Large Object就可以存儲(chǔ)大文件,存儲(chǔ)的方式是在單獨(dú)的一張表中存儲(chǔ)大文件,然后通過(guò)oid在當(dāng)前表中進(jìn)行引用;下面通過(guò)一個(gè)例子來(lái)解釋?zhuān)?/p>
CREATE TABLE imageslo (imgname text, imgoid oid);
首先是創(chuàng)建一張表,該表中第二個(gè)字段類(lèi)型為oid,之后就是通過(guò)該字段引用大文件對(duì)象;下面我們?cè)诒碇胁迦胍粡垐D片:
// All LargeObject API calls must be within a transaction block
conn.setAutoCommit(false);
// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();
// Create a new large object
int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);
// Open the large object for writing
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);
// Now open the file
File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);
// Copy the data from the file to the large object
byte buf[] = new byte[2048];
int s, tl = 0;
while ((s = fis.read(buf, 0, 2048)) > 0) {
obj.write(buf, 0, s);
tl += s;
}
// Close the large object
obj.close();
// Now insert the row into imageslo
PreparedStatement ps = conn.prepareStatement("INSERT INTO imageslo VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setInt(2, oid);
ps.executeUpdate();
ps.close();
fis.close();
在代碼中需要使用lobp.open打開(kāi)一個(gè)大文件,然后將圖片的內(nèi)容寫(xiě)入這個(gè)對(duì)象當(dāng)中;下面從大文件對(duì)象中讀取這個(gè)圖片:
// All LargeObject API calls must be within a transaction block
conn.setAutoCommit(false);
// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();
PreparedStatement ps = con.prepareStatement("SELECT imgoid FROM imageslo WHERE imgname = ?");
ps.setString(1, "myimage.gif");
ResultSet rs = ps.executeQuery();
if (rs != null) {
while (rs.next()) {
// Open the large object for reading
int oid = rs.getInt(1);
LargeObject obj = lobj.open(oid, LargeObjectManager.READ);
// Read the data
byte buf[] = new byte[obj.size()];
obj.read(buf, 0, obj.size());
// Do something with the data read here
// Close the object
obj.close();
}
rs.close();
}
ps.close();
需要注意的是,對(duì)于Large Object的操作都需要放在一個(gè)事務(wù)(Transaction)當(dāng)中;如果要?jiǎng)h除大文件所在行,在刪除這行之后,還需要再執(zhí)行刪除大文件的操作
注:使用Large Object會(huì)有安全問(wèn)題,連接到數(shù)據(jù)庫(kù)的用戶(hù),即便沒(méi)有包含大對(duì)象所在列的權(quán)限,也可以操作這個(gè)大對(duì)象
Beego orm如何存儲(chǔ)圖片
看完上面的postgres對(duì)于圖片的存儲(chǔ),再來(lái)看下如何使用beego orm存儲(chǔ)一張圖片;在beego orm中支持了go的所有基礎(chǔ)類(lèi)型,但是不支持slice;所以,不能直接將[]byte映射到bytea字段上面
好在beego orm提供了一個(gè)Fielder接口,可以自定義類(lèi)型,接口定義如下:
// Fielder define field info
type Fielder interface {
String() string
FieldType() int
SetRaw(interface{}) error
RawValue() interface{}
}
所以,現(xiàn)在就需要定義一個(gè)字節(jié)數(shù)組的類(lèi)型,然后實(shí)現(xiàn)這些接口就好了,代碼如下:
type ByteArrayField []byte
// set value
func (e *ByteArrayField) SetRaw(value interface{}) error {
if value == nil {
return nil
}
switch d := value.(type) {
case []byte:
*e = d
case string:
*e = []byte(d)
default:
return fmt.Errorf("[ByteArrayField] unsupported type")
}
return nil
}
func (e *ByteArrayField) RawValue() interface{} {
return *e
}
// specified type
func (f *ByteArrayField) FieldType() int {
return orm.TypeTextField
}
func (f *ByteArrayField) String() string {
return string(*f)
}
然后,我們就可以在struct中進(jìn)行映射了,如下:
type ImageModel struct{
ImageName string `orm:"column(image_name)"`
ImageData ByteArrayField `orm:"column(image_data);type(bytea)"`
}
這樣就可以使用orm的接口操作imageModel,向數(shù)據(jù)庫(kù)插入圖片,或者從數(shù)據(jù)庫(kù)讀出圖片的內(nèi)容了
以上就是詳解如何使用beego orm在postgres中存儲(chǔ)圖片的詳細(xì)內(nèi)容,更多關(guān)于beego orm postgres存儲(chǔ)圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
golang定時(shí)任務(wù)cron項(xiàng)目實(shí)操指南
Go實(shí)現(xiàn)的cron 表達(dá)式的基本語(yǔ)法跟linux 中的 crontab基本是類(lèi)似的,下面這篇文章主要給大家介紹了關(guān)于golang定時(shí)任務(wù)cron項(xiàng)目實(shí)操的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Go并發(fā)編程中的錯(cuò)誤恢復(fù)機(jī)制與代碼持續(xù)執(zhí)行實(shí)例探索
這篇文章主要為大家介紹了Go并發(fā)編程中的錯(cuò)誤恢復(fù)機(jī)制與代碼持續(xù)執(zhí)行實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Go基于GORM 獲取當(dāng)前請(qǐng)求所執(zhí)行的 SQL 信息(思路詳解)
這篇文章主要介紹了Go基于GORM 獲取當(dāng)前請(qǐng)求所執(zhí)行的 SQL 信息(思路詳解),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Go語(yǔ)言atomic.Value如何不加鎖保證數(shù)據(jù)線(xiàn)程安全?
這篇文章主要介紹了Go語(yǔ)言atomic.Value如何不加鎖保證數(shù)據(jù)線(xiàn)程安全詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
golang實(shí)現(xiàn)大文件上傳功能全過(guò)程
Go語(yǔ)言可以用來(lái)實(shí)現(xiàn)大文件傳輸,下面這篇文章主要給大家介紹了關(guān)于golang實(shí)現(xiàn)大文件上傳功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07

