在 DocumentDB 中创建附件时出现无效请求负载错误

Invalid request payload error when creating attachment in DocumentDB

我正在使用文档数据库,有些文档可能会变得很大,以至于超过大小限制 (2MB)。我正在尝试将大块存储为附件,这是我的代码片段:

var stringifiedItems = JsonConvert.SerializeObject(doc.Items);
var attachmentStream = new MemoryStream(Encoding.UTF8.GetBytes(stringifiedItems));
doc.Attachment = await _client.CreateAttachmentAsync(GetDocumentLink(docId), attachmentStream, new MediaOptions()
    {
        ContentType = "application/json",
        Slug = "Items.json"
    });

但是 CreateAttachmentAsync() 调用总是抛出一般错误:

"The request payload is invalid. Ensure to provide a valid request payload."

有人可以阐明我的代码遗漏了什么,或者就如何解决问题提出建议吗?

当我们通过 MediaOptions 设置 ContentType 时,它会设置 Content-Type HTTP 请求 headers。但是,带有 MemoryStream 参数的方法 CreateAttachmentAsync 将发送没有 body 数据的请求,因此这是问题负载无效的路由原因。

要解决此问题,我们可以使用 application/octet-stream 来上传流 objects,而不是将 ContentType 设置为 application/json