Hello @Brian Ho ,
Here is the complete piece of C#.net code to download a blob from a container. I have successfully tested and able to download a blob using SAS Key.
Reference Code: https://gist.github.com/dazfuller/9b5ea145525879fb5114bd78b53e9ee5#file-azureblobdownload-cs
Please Note: If you get 403 Forbidden error, make sure to generate the SAS key with all the right permissions.
Fully tested code:
public static void DownloadFilesUsingSAS()
{
const string accountName = "storageaccountname";
const string blobContainerName = "test";
const string downloadLocation = @"C:\data";
const string sasToken = @"SASTOKEN";
// Create a credential object from the SAS token then use this and the account name to create a cloud storage connection
var accountSAS = new StorageCredentials(sasToken);
var storageAccount = new CloudStorageAccount(accountSAS, accountName, null, true);
// Get the Blob storage container
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(blobContainerName);
// Fail if the container does not exist
if (!container.Exists())
{
Console.Error.WriteLine($"Can't find container '{blobContainerName}'");
return;
}
// Get the list of blobs in the container, just for demo purposes this is ordered by blob name and taking only
// the first 10 items
var blobs = container
.ListBlobs(blobListingDetails: BlobListingDetails.Metadata)
.OfType<CloudBlob>()
.OrderBy(blobItem => blobItem.Name)
.Take(10)
.ToList();
// Set the request options to ensure we're performing MD5 validation
var downloadOptions = new BlobRequestOptions
{
DisableContentMD5Validation = false,
UseTransactionalMD5 = true
};
// Download the files
// The parameters are named for reference only and can safely be removed
Task.WaitAll(
blobs.Select(
blobItem => blobItem.DownloadToFileAsync(
path: Path.Combine(downloadLocation, blobItem.Name),
mode: FileMode.Create,
accessCondition: null,
options: downloadOptions,
operationContext: null)).ToArray());
}
If the above answer helps out , kindly Upvote & Accept the answer.
Regards,
Shiva.