locked
Blob Storage DownloadToStream RRS feed

  • Question

  • Hello, 

    I’m trying to create an azure service that stores some gpx files in Azure blob storage. I’m not sure how to do it… 

    This is my code:

    CloudBlobContainer cont = client.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("ContainerName"));

    Stream stream = File.Create("TempFile.gpx");           

    cont.GetBlobReference(track).DownloadToStream(stream);

    XDocument gpxDoc = XDocument.Load(stream); 

    But I prefer not have to create a temporary file to download the content of the blob. I have tried using MemoryStream, but I got an error when I try to load the stream to XDocument. Here is the code:

    CloudBlobContainer cont = client.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("ContainerName"));

    MemoryStream st = new MemoryStream();

    cont.GetBlobReference(track).DownloadToStream(st);

    XDocument gpxDoc = XDocument.Load(st);  à XmlException Root element is missing 

    Any solution?

    Thanks,

    Thursday, December 15, 2011 4:12 PM

Answers

  • This is a great scenario for a BlobReadStream. Essentially call OpenRead() on the blob reference, seek the stream if neccessary, and then simply pass it into the xmld code, this will not use any intermediary files /buffers aside from the current blob block being accessed.

     

    A side note, the reason your second sample is not working is that the stream is positioned at the end of the data when its passed into load, reset the position and that will work as well.

    joe

    Thursday, December 15, 2011 11:03 PM

All replies

  • This is a great scenario for a BlobReadStream. Essentially call OpenRead() on the blob reference, seek the stream if neccessary, and then simply pass it into the xmld code, this will not use any intermediary files /buffers aside from the current blob block being accessed.

     

    A side note, the reason your second sample is not working is that the stream is positioned at the end of the data when its passed into load, reset the position and that will work as well.

    joe

    Thursday, December 15, 2011 11:03 PM
  • Thanks Joe! You were right, the reason the second sample was not working was that the stream was positioned at the end. Now, it works:

     

    CloudBlobContainer cont = client.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("GPXContainerName"));

    MemoryStream st = new MemoryStream();

    cont.GetBlobReference(track).DownloadToStream(st);

    st.Seek(0, SeekOrigin.Begin);

    XDocument gpxDoc = XDocument.Load(st);

    Friday, December 23, 2011 8:34 AM