I am using Azure.Storage.Blobs version=12.4.1. I have a REST endpoint that I want to use to download blobs from a storage account.
I need to stream the result to a HttpResponseMessage and I do not want to use a MemoryStream. I want to stream the result directly to the calling client. Is there a way to achieve this. How to get the downloaded blob in the HttpResponseMessage content? I do not want to use MemoryStream, since there will be a lot of download requests.
The BlobClient class has a method DownloadToAsync but it requires a Stream as a parameter.
var result = new HttpResponseMessage(HttpStatusCode.OK);
var blobClient = container.GetBlobClient(blobPath);
if (await blobClient.ExistsAsync())
{
var blobProperties = await blobClient.GetPropertiesAsync();
var fileFromStorage = new BlobResponse()
{
ContentType = blobProperties.Value.ContentType,
ContentMd5 = blobProperties.Value.ContentHash.ToString(),
Status = Status.Ok,
StatusText = "File retrieved from blob"
};
await blobClient.DownloadToAsync(/*what to put here*/);
return fileFromStorage;
}
You could simply create a new memory stream and download the blob's content to that stream.
Something like:
var connectionString = "UseDevelopmentStorage=true";
var blobClient = new BlockBlobClient(connectionString, "test", "test.txt");
var ms = new MemoryStream();
await blobClient.DownloadToAsync(ms);
ms
will have the blob's contents. Don't forget to reset memory stream's position to 0
before using it.