Looking for a C# sample to upload/download file by given a Shared Access Signature (SAS) key

Brian Ho 26 Reputation points
2021-11-23T05:05:57.77+00:00

Hi,

I found the link below link using C# to upload and download file. It is using a ConnectionString to establish the connection to Azure.
https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet

I am looking for a sample to do the same by given an Azure Shared Access Signature (SAS). If anyone has a sample and would not mind sharing, I really appreciate it.

Thanks,
Brian.

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,201 questions
0 comments No comments
{count} votes

Accepted answer
  1. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2021-11-23T07:51:20.997+00:00

    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.


1 additional answer

Sort by: Most helpful
  1. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2021-11-24T23:21:24.723+00:00

    Hello @Brian Ho ,
    Can you try installing Microsoft.WindowsAzure Nuget Packages using the solution explorer ?
    Go to Solution Explorer -> Select References -> Right Click -> Manage Nuget Packages -> Search for WindowsAzure.Storage
    In the search list, install the first one i.e. WindowsAzure.Storage.

    152250-image.png

    Regards,
    Shiva.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.