Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing MemoryStream File to Azure Blob

I have an image that gets dynamically generated via System.Drawing. I am then outputting the generated image to a MemoryStream for storage into my Azure blob.

But I can't seem to get my file to store in the blob of my choice. There are no errors occurring and my image is successfully getting saved to MemoryStream. As expected, my blob is empty.

I have ensured my blob container has public read/write access.

Code

// Retrieve storage account from connection string.
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()));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("myimagecontainer");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.jpg");
            
//Output image
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); //GenerateImage.Render() method creates a custom image and returns a Bitmap
            
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = msImage)
{
    blockBlob.UploadFromStream(fileStream);
}

Any help would be appreciated.

Update

I managed to find what the main 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 "Gaurav Mantri" response as correct. If it wasn't for his insight, my image would not have uploaded to the blob.

like image 476
sbhomra Avatar asked Feb 21 '13 10:02

sbhomra


2 Answers

Try and set the Position of your memory stream to 0 and see if that helps. Something like the code below:

msImage.Position = 0;//Move the pointer to the start of stream.

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = msImage)
{
    blockBlob.UploadFromStream(fileStream);
}
like image 84
Gaurav Mantri Avatar answered Nov 01 '22 19:11

Gaurav Mantri


I had the same problem, the code works but the pdf file size was always 0, I had to make the MyMemoryStream.Position = 0; below is the code I have used. Hope it helps someone.

using (pdfStream)
{
    pdfStream.Position = 0;
    blob.UploadFromStream(pdfStream);                            
    _model.ConvertedPdfUrl = blob.Uri.AbsoluteUri;
}
like image 2
Ananth Asamani Avatar answered Nov 01 '22 19:11

Ananth Asamani