使用 C# 从 CouchDB 中获取大的二进制数据

Fetch big binary data from CouchDB using c#

我使用 CouchDB 作为内容服务器来存储图像和大型视频文件 问题是当我检索这些文件时,它表示为文档响应,它需要反序列化以获取二进制文件,这将导致 内存不足错误 (对于任何大于 300mb 的文件)。

这是上传功能

  public async Task<string> InsertDataToCouchDb(string dbName, string id, string recodNo, string filename, byte[] image)
    {
        var connection = System.Configuration.ConfigurationManager.ConnectionStrings["CouchDb"].ConnectionString;
        using (var db = new MyCouchClient(connection, dbName))
        {

            // HERE I NEED TO UPLOAD MY IMAGE BYTE[] AS CHUNKS 
            var artist = new couchdb
            {
                _id = id,
                filename = filename,
                Image = image
            };

            var response = await db.Entities.PutAsync(artist);
            return  response.Content._id;
        }

    }

下载功能

   public async Task<byte[]> FetchDataFromCouchDb(string dbName, string id)
    {
        var connection = System.Configuration.ConfigurationManager.ConnectionStrings["CouchDb"].ConnectionString;
        using (var db = new MyCouchClient(connection, dbName))
        {
            //HERE I NEED TO RETRIVE MY FULL IMAGE[] FROM CHUNKS
            var test = await db.Documents.GetAsync(id, null);

            var doc = db.Serializer.Deserialize<couchdb>(test.Content);
            return doc.Image;

        }
    }

我需要直接获取二进制数据而不像mongoDB GridFSBucket

那样进行任何反序列化

谢谢

您做错了,必须使用附件对象将附件与文档分开。

var response = await db.Entities.PutAsync(artist);//The document
                var request = new PutAttachmentRequest(
                    id,
                    response.Rev,//from the added document
                    "filename",
                    HttpContentTypes.Text,
                    filebytes);

                var xxx = await db.Attachments.PutAsync(request);

//这里是读取文件的方法 AttachmentResponse resAtt = await db.Attachments.GetAsync(id, "filename");

                var filebytes2 = resAtt.Content;//no deserialize