I am having a hard time figuring out how to download a blob from Azure storage using .net and a generated Shared Access Signature token.
First of all, most of the tutorials I could find are using Microsoft.Azure.Management.Storage
nuget package, which is deprecated. Instead I use the newest Azure.Storage.Blobs
package.
I have the following code so far. I don't know what to do with the generated SAS token. How do I download the blob from there ?
[HttpGet]
public async Task<IActionResult> Download([FromQuery] string blobUri)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(this.configuration.GetConnectionString("StorageAccount"));
// Create a SAS token that's valid for one hour.
BlobSasBuilder sasBuilder = new BlobSasBuilder()
{
BlobContainerName = "my container name",
BlobName = blobUri,
Resource = "b",
StartsOn = DateTimeOffset.UtcNow,
ExpiresOn = DateTimeOffset.UtcNow.AddHours(1)
};
// Specify read permissions for the SAS.
sasBuilder.SetPermissions(BlobSasPermissions.Read);
// Use the key to get the SAS token.
var sasToken = sasBuilder.ToSasQueryParameters(new Azure.Storage.StorageSharedKeyCredential("my account name", "my account key"));
BlobClient blob = blobServiceClient.GetBlobContainerClient("my container name").GetBlobClient($"{blobUri}");
await using (MemoryStream memoryStream = new MemoryStream())
{
await blob.DownloadToAsync(memoryStream);
return File(memoryStream, "file");
}
}
blobUri
parameter? Is it the name of the blob or it's URL? – Gaurav Mantri