Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uploading to azure storage from memorystream returning an empty file

I'm writing a System.Drawing.Bitmap to Azure storage using a memory stream. I have the correct credentials and everything azure side wires up correctly. I have uploaded an image into a blob successfully using an input stream so I think it must be a problem with how i am using the memorystream object.

After looking about for a while, the general solution to my problem looked to be setting the memorystream position to 0, that hasn't worked for me however and an empty file is still being saved into azure.

my code is:

 using (image)
 {
 System.IO.MemoryStream ms = new MemoryStream();

 //create an encoder parameter for the image quality
 EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

 //get the jpeg codec
 ImageCodecInfo imgCodec = ImageUtilities.GetEncoderInfo(CodecInfo);

  //create a collection of all parameters that we will pass to the encoder
  EncoderParameters encoderParams = new EncoderParameters(1);

  //set the quality parameter for the codec
  encoderParams.Param[0] = qualityParam;

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

  image.Save(ms, imgCodec, encoderParams);

  blockBlob.UploadFromStream(ms);
}

The image element that is saved at the end has data in it. at debug is holds the correct length etc. so the problem is somewhere in the uploading step

like image 876
LiamHT Avatar asked Dec 11 '14 08:12

LiamHT


People also ask

How do I upload a file to Azure Blob storage using REST API?

Upload contents of a folder to Data Box Blob storage To get your account key, in the Azure portal, go to your storage account. Go to Settings > Access keys, select a key, and paste it into the AzCopy command. If the specified destination container does not exist, AzCopy creates it and uploads the file into it.


1 Answers

You have to rewind your memory Stream after saving the image and before uploading :

  //...

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

  image.Save(ms, imgCodec, encoderParams);
  //HERE !!!
  ms.Position = 0;

  blockBlob.UploadFromStream(ms);
}
like image 159
Guillaume Avatar answered Sep 29 '22 07:09

Guillaume