Wednesday, 2 August 2017

Azure: Why is my blob 0KB when I move it?

I was archiving documents in Blob Storage and found that the target files were always being rendered as 0 KB.

I was using the following code:

var storageAccount = CloudStorageAccount.Parse("CONNECTIONSTRING");
var blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference("container");
var blob = blobContainer.GetBlobReference("myfilename")

// Get the source as a stream
Stream stream = new MemoryStream();
blob.DownloadToStream(stream);

var destBlob = blobContainer.GetBlockBlobReference(blob.Name);
destBlob.UploadFromStream(stream);

blob.Delete();

The code should work, except for the fact that the stream needs to be repointed to the start for the download to work.

blob.DownloadToStream(stream); (from above)
stream.Position = 0;

And now the copy/move works

No comments:

Post a Comment