简体   繁体   中英

Cannot download Azure Blob to stream

I'm using Azure Blob Storage to save some files. I'm having issues downloading this stream & am not sure why. I don't get any errors, just an empty stream. I've verified that the file exists in the container, and even ran code to list all files in the container. Any help would be greatly appreciated.

private async Task<MemoryStream> GetMemoryStreamAsync(string fileName)
{
    var storageAccountName = Environment.GetEnvironmentVariable("storage_account_name");
    var storageAccountKey = Environment.GetEnvironmentVariable("storage_access_key");
    var storageContainerName = Environment.GetEnvironmentVariable("storage_container_name");

    CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, storageAccountKey), true);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(storageContainerName);
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

    MemoryStream stream = new MemoryStream();

    await blockBlob.DownloadToStreamAsync(stream);

    return stream;
}

You'll need to set the position to zero before returning the stream so that the consumer of the stream reads it from the beginning.

MemoryStream stream = new MemoryStream();

await blockBlob.DownloadToStreamAsync(stream);

stream.Position = 0;

return stream;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM