【问题标题】:How can I store Images in Azure Cosmos DB?如何在 Azure Cosmos DB 中存储图像?
【发布时间】:2018-08-26 20:59:10
【问题描述】:

我有 PNG 图像,我想将其与一些元数据一起存储在 cosmos db 中。最好的存储方法是什么?我不想单独存储在 Azure blob 存储中,最好与数据一起存储。

【问题讨论】:

    标签: azure azure-cosmosdb


    【解决方案1】:

    如果你想将图片直接存储到cosmos db,可以使用DocumentClient.CreateAttachmentAsync方法直接存储。

    using (FileStream fileStream = new FileStream(@".\something.pdf", FileMode.Open))
    {
        //Create the attachment
        Attachment attachment = await client.CreateAttachmentAsync("dbs/db_rid/colls/coll_rid/docs/doc_rid/attachments/", 
        fileStream, 
        new MediaOptions 
        { 
           ContentType = "image/jpeg", 
           Slug = "myImage.jpeg" 
        });
    }
    

    管理外部存储的附件没有任何固有的内置功能。相反,由您来存储它们然后引用它们。

    最常见的模式是将 URL 存储到特定附件和文档中(例如存储到 Azure 存储中的 blob)。

    注意:通常您将图像添加到 CosmosDB 中,查询会花费很多。推荐的做法是将图像存储在 Azure Blob 中并在 Cosmos DB 文档中添加链接,您可以利用 Azure CDN 之类的东西,这将使您的图像在您的移动设备上加载得更快。

    【讨论】:

    • 谢谢@sajeetharan,我想从客户端上传一张图片并通过api发送到服务器以将其存储在数据库中。另外,我不想将我的图像公开。我认为 blob 网址是公开的。所以只关心安全。处理我提到的场景的最佳方法是什么。
    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2019-11-17
    相关资源
    最近更新 更多