I have the following code:
public static void UploadStreamToBlob(Stream stream, string containerName, string blobName)
{
    CloudStorageAccount storageAccount = 
        CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
    blobContainer.CreateIfNotExists();
    blobContainer.SetPermissions(
        new BlobContainerPermissions
        {
            PublicAccess = BlobContainerPublicAccessType.Blob
        });
    CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
    long streamlen = stream.Length;  <-- This shows 203 bytes
    blockBlob.UploadFromStream(stream);        
}
and
public static Stream DownloadStreamFromBlob(string containerName, string blobName)
{
    CloudStorageAccount storageAccount = 
        CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
    Stream stream = new MemoryStream();
    CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
    if (blockBlob.Exists())
    {
        blockBlob.DownloadToStream(stream);
        long streamlen = stream.Length;  <-- This shows 0 bytes
        stream.Position = 0;          
    }
    return stream;
}
I'm running this in the Azure emulator, which I have pointed to my Sql Server.
From what I can tell, it appears that the UploadFromStream is sending the data correctly, however, if I try and run the DownloadStreamFromBlob, it returns a 0 length stream. The blockBlob.Exists is returning true, so I assume it's there. I just can't figure out why my stream is empty.
Btw, I'm passing in test and test for containerName and blobName on both calls.
Any ideas?
Ah, I figured it out...
The following lines:
long streamlen = stream.Length;
blockBlob.UploadFromStream(stream);   
need to be changed to
long streamlen = stream.Length;  
stream.Position = 0;
blockBlob.UploadFromStream(stream);   
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With