Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store Images in Azure Cosmos DB?

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 don't want to store in Azure blob storage separately, better to store with the data.

like image 993
Ashish Avatar asked Mar 18 '18 01:03

Ashish


People also ask

Can images be stored in Cosmos DB?

If you want to store images directly to the cosmos db, you can use DocumentClient. CreateAttachmentAsync method to store it directly. There's nothing inherently built-in to manage externally-stored attachments. Rather, it's up to you to store them and then reference them.

How do I store images in Azure storage?

Sign in to the Azure portal. From the left menu, select Storage accounts, then select the name of your storage account. Select Containers, then select the images container. Verify the image is shown in the container.

Can we store files in Cosmos DB?

Attachments aren't supported in all versions of the Azure Cosmos DB's SDKs. Managed attachments are limited to 2 GB of storage per database account.

How do I upload images to BLOB storage?

Run project locally to verify connection to Storage account. Your SAS token and storage account name are set in the src/azure-storage-blob. ts file, so you are ready to run the application. Select an image from the images folder to upload then select the Upload!


1 Answers

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.

like image 50
Sajeetharan Avatar answered Sep 22 '22 08:09

Sajeetharan