如何在azure cosmos db中插入图像(Document DB)

-3 c# azure xamarin uwp azure-cosmosdb

我正在使用带有UWP的xamarin,我想将图像插入azure cosmos DB(Document DB).

那我该怎么做呢?

dav*_*der 8

一些代码

var myDoc = new { id = "42", Name = "Max", City="Aberdeen" }; // this is the document you are trying to save
var attachmentStream = File.OpenRead("c:/Path/To/File.pdf"); // this is the document stream you are attaching

var client = await GetClientAsync();
var createUrl = UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName);
Document document = await client.CreateDocumentAsync(createUrl, myDoc);

await client.CreateAttachmentAsync(document.SelfLink, attachmentStream, new MediaOptions()
    {
        ContentType = "application/pdf", // your application type
        Slug = "78", // this is actually attachment ID
    });
Run Code Online (Sandbox Code Playgroud)

来源:代码来源

我怎么发现: 研究链接

以一种不太有趣的方式,而是在一个不会过期的链接中,并为您提供指向正确方向的其他结果.

======

我使用azure,如果你将图像放入documentDb,你将为查询成本付出高昂代价.它是内置的,但基本上通常的做法是使用Azure blob并将链接保存在字段中,然后有一个viewmodel,它将返回链接(如果是公共的)或二进制数据(如果不是).

通过绑定附件,您可以进一步将代码固定到仅能够在DocumentDb上工作.

  • 添加到Dave的答案,如果您将图像存储在Azure Blob中并在Cosmos DB文档中添加链接,您可以利用[Azure CDN]之类的内容(https://docs.microsoft.com/en-us/azure/ cdn/cdn-create-a-storage-account-with-cdn)这将使您的图像加载到您的移动客户端更快. (3认同)