【问题标题】:How to insert image in azure cosmos db (Document DB) [closed]如何在 azure cosmos db(文档 DB)中插入图像 [关闭]
【发布时间】:2018-03-02 21:45:31
【问题描述】:

我将 xamarinUWP 一起使用,我想将图像插入到 azure cosmos DB(Document DB)

那么我该如何实现呢?

【问题讨论】:

  • 创建 Cosmos DB 时,您选择一个 API。你用的是哪一个?
  • 我选择了 SQL(DocumentDB) 并希望将图像存储到其中。
  • 您介意分享使用 documentdb 存储图像的原因吗?如果 Azure blob 存储是可以接受的,我们可以使用 Azure storage blob 来做到这一点。
  • 是的 azure storage blob 也一样,但我想将图像存储/插入到数据库中
  • 如果要使用Azure documentdb附件,请参考this和这个link

标签: c# azure xamarin uwp azure-cosmosdb


【解决方案1】:

一些代码

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
    });

来源:Source of code

我是如何发现的:Link for research

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

======

我用的是azure,如果你把图片放到documentDb,你会付出高昂的查询成本。它是内置的,但本质上,使用 Azure blob 并将链接保存在字段中,然后有一个视图模型将返回链接(如果它是公共的)或二进制数据(如果不是)。

通过绑定附件,您进一步将代码固定为只能在 DocumentDb 上工作。

【讨论】:

  • 添加到 Dave 的答案中,如果您将图像存储在 Azure Blob 中并在 Cosmos DB 文档中添加链接,您可以利用 Azure CDN 之类的东西,这将使您的图像在您的移动设备上加载得更快客户。
猜你喜欢
  • 1970-01-01
  • 2018-11-15
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-23
  • 2018-04-13
  • 1970-01-01
相关资源
最近更新 更多