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

yzckvree  于 2022-12-09  发布在  CouchDB
关注(0)|答案(1)|浏览(234)

我使用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那样进行任何反序列化
谢谢你

xtfmy6hx

xtfmy6hx1#

您的方法不对,必须使用Attachment对象将附件与文档分开。

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 = awaitdb.Attachments.getAsync(id,“文件名”);

var filebytes2 = resAtt.Content;//no deserialize

相关问题