如何将图像插入CouchDB

问题描述 投票:0回答:1

我试图弄清楚如何使用在这里找到的node-CouchDB库将图像插入CouchDB:https://www.npmjs.com/package/node-couchdb

这是我所做的:

fs.readFile('download.jpeg', (err, data) => {
        binary_data = new Buffer(data, 'binary');
        couch.insertAttachment("node_db", doc_number, "download.jpeg", binary_data, rev_number).then(({data, headers, status}) => {

        }, err => {
            console.log("ERROR"+ err.code);
        });
});

结果是CouchDB以如下格式存储文件:

{
  "_id": "2741d6f37d61d6bbdf63df3be5000504",
  "_rev": "22-bfdbe6db35c7d9873a2cc8a38afb2833",
  "_attachments": {
    "attachment": {
      "content_type": "application/json",
      "revpos": 22,
      "digest": "md5-on0A+d7045WPI6FyS1ut4g==",
      "length": 22482,
      "stub": true
    }
  }
}

//This is what the data looks like in CouchDB using the View Attachment Function through the interface:

{"type":"Buffer","data":[255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,255,219,0,132,0,9,6,7,18,18,18,21,18,19,19,22,21,21,23,23,23,24,21,21,21,23,23,21,21,24,21,21,21,23,22,22,21,21,22,24,29,40,32,24,26,37,29,21,21,33,49,33,37,41,43,46,46,46,23,31,51,56,51,45,55,40,45,46,43,1,10,10,10,14,13,14,26,16,16,26,45,37,29,37,45,45,45,45,45,45,45,241,...]

然后我尝试在请求的标题中将Content-Type属性更改为“ image / jpeg”,结果是:

{
  "_id": "2741d6f37d61d6bbdf63df3be5000504",
  "_rev": "23-cf8c2076b43082fdfe605cad68ef2355",
  "_attachments": {
    "attachment": {
      "content_type": "image/jpeg",
      "revpos": 23,
      "digest": "md5-SaekQP37DCCeGX2M8UVeGQ==",
      "length": 22482,
      "stub": true
    }
  }
}

但是,这仍然会导致在CouchDB界面中无法查看图像(单击查看附件)。在这种情况下,图像的大小仅为6,904字节,但存储时的长度约为22k(夸大了CouchDB中的大小),因此我假设我没有将图像的正确表示(编码)传递给CouchDB 。

node.js couchdb node-modules
1个回答
0
投票

您可以将图像数据编码为base64字符串并保存,尽管我一点也不推荐。我要做的就是将文件上传到对象存储(例如AWS S3或它的开源替代MinIO),然后仅将对文件的引用(例如,图像URL)保存在数据库中。

P.S .:很抱歉我的答案中缺少链接和参考,我正在用手机编写它。我可以在回家时立即对其进行编辑并包括参考。

© www.soinside.com 2019 - 2024. All rights reserved.