How can I store Images in Azure Cosmos DB?

懵懂的女人 提交于 2019-12-18 07:40:35

问题


I have PNG images which i want to store in cosmos db along with some metadata. What is the best way to store it ? I dont want to store in Azure bolb storage separately, better to store with the data.


回答1:


If you want to store images directly to the cosmos db, you can use DocumentClient.CreateAttachmentAsync method to store it directly.

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

There's nothing inherently built-in to manage externally-stored attachments. Rather, it's up to you to store them and then reference them.

The most common pattern is to store a URL to the specific attachment, with a document (e.g. to a blob in Azure Storage).

NOTE: Generally you are adding images into CosmosDB, it will costs you much for queries. Recommended way is to do is to store the image in Azure Blob and add the link in the Cosmos DB document you can take advantage of things like Azure CDN that will make your images load faster for your mobile.




回答2:


There is a limitation for the total size of all attachments..

I published a blog article which shows a comparison.

https://docdb.info/azure-cosmos-db-document-attachment-storage-comparison/

I am about to publish an article which shows how you can use a Function App to receive a Form-Post an saves the file attachment.



来源:https://stackoverflow.com/questions/49343675/how-can-i-store-images-in-azure-cosmos-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!