I have an image that is dynamically generated through System.Drawing. Then I displayed the generated image MemoryStreamfor storage in my Azure block.
But I can not force my file to be stored in the blob of my choice. No errors occur, and my image is successfully saved to MemoryStream. As expected, my blob is empty.
I guaranteed that my blob container has open read / write access.
the code
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureAccountName").ToString(), Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureSharedKey").ToString()));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("myimagecontainer");
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.jpg");
ImageCodecInfo[] Info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1);
Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
System.IO.MemoryStream msImage = new System.IO.MemoryStream();
GenerateImage.Render(imageOutput).Save(msImage, Info[1], Params);
using (var fileStream = msImage)
{
blockBlob.UploadFromStream(fileStream);
}
Any help would be appreciated.
Update
I managed to find the root cause of the error. I needed to change the following:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureAccountName").ToString(), Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureSharedKey").ToString()));
to
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DiagnosticsConnectionString"));
But I will mark the answer of “Gaurav Mantry” as correct. If not for his understanding, my image would not be uploaded to blob.
source