0
votes

First of all, i am new in azure development, so my question will be ambiguous. i need better suggestion. I have web api C# that receive binary data (image). I want to write it into azure blob storage. In my case, i need to check if blob storage exist then write the image else create the directory and write the image.

This directory is public read access so that i can reflect image in any public app.

Following method store the image and return the image public path

public class BlobStorageService : IBlobStorageService
    {
        public async Task<string> SaveSingleImage(byte[] image)
        {
            throw new NotImplementedException();
        }
    }
1
sorry, but what exactly is the question? - Guru Pasupathy
@GuruPasupathy They want us to write the code for them? ;) - Caius Jard
not exactly the code but the way to solve this problem - Mashhad Saleem

1 Answers

0
votes

I have done it. Following is my code. This will take image byte and save into blob storage and return the public URL of that image

public async Task<string> GoAppSaveSingleImage(ProofImageViewModel proofImage)
        {

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(GlobalConfig.Instance.AzureBlobStorageForGoAppImages);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(GlobalConfig.Instance.AzureBlobGoAppImagesContainerName);
            CloudBlockBlob blockBlob = container.GetBlockBlobReference($"{proofImage.CountryCode}/{DateTime.Now.Year}/{DateTime.Now.Month}/{proofImage.ProofImage}");
            blockBlob.Properties.ContentType = "image/png";
            if (GlobalConfig.Instance.AzureBlobGoAppImagesIsStorageTier)
            {
                blockBlob.SetStandardBlobTierAsync((StandardBlobTier)GlobalConfig.Instance.AzureBlobGoAppImagesStorageTier).GetAwaiter().GetResult();
            }
            using (var stream = new MemoryStream(proofImage.ProofImage, writable: false))
            {
                await blockBlob.UploadFromStreamAsync(stream);
            }
            string publicUrl = blockBlob.Uri.AbsoluteUri;
            return publicUrl;
        }