2

I am trying to serialize a DataTable to XML and then upload it to Azure blob storage.

The below code works, but seems clunky and memory hungry. Is there a better way to do this? I'm especially referring to the fact that I am dumping a memory stream to a byte array and then creating a new memory stream from it.

        var container = blobClient.GetContainerReference("container");
        var blockBlob = container.GetBlockBlobReference("blob");

        byte[] blobBytes;
        using (var writeStream = new MemoryStream())
        {
            using (var writer = new StreamWriter(writeStream))
            {
                table.WriteXml(writer, XmlWriteMode.WriteSchema);

            }
            blobBytes = writeStream.ToArray();
        }
        using (var readStream = new MemoryStream(blobBytes))
        {
            blockBlob.UploadFromStream(readStream);
        }
Tom Ferguson
  • 907
  • 1
  • 10
  • 26
  • As it's table data already have you considered uploading to a table in the storage account? Or does your solution require you use XML? – Martyn C May 18 '16 at 17:32
  • @MartynC It doesn't have to be XML, but it does have to be in blob storage and it does have to be easily de-serializable back in to a DataTable – Tom Ferguson May 19 '16 at 08:40

1 Answers1

4

New answer:

I've learned of a better approach, which is to open a write stream directly to the blob. For example:

        using (var writeStream = blockBlob.OpenWrite())
        {
            using (var writer = new StreamWriter(writeStream))
            {
                table.WriteXml(writer, XmlWriteMode.WriteSchema);

            }
        }

Per our developer, this does not require the entire table to be buffered in-memory, and will probably encounter less copying around of data.

Original answer:

You can use the CloudBlockBlob.UploadFromByteArray method, and upload the byte array directly, instead of creating the second stream.

See https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.uploadfrombytearray.aspx for the method syntax.

  • Just what I was looking for! Seems obvious now. Thank you! – Tom Ferguson May 25 '16 at 08:18
  • I like this. I changed my implementation slightly. Why use the StreamWriter? OpenWrite returns a CloudBlobStream which inherits from Stream. You can pass in that directly to WriteXml without using an additional nested `using` statement. – Rick Glos Nov 15 '16 at 19:36