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.
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.
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.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With