Created
April 24, 2021 22:48
-
-
Save SeanDrum/8e7f7387f0aa5e1a9f9d435ea27b4947 to your computer and use it in GitHub Desktop.
A simple example of how to download all files in a Azure Blob Container using a SAS Auth Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Configuration; | |
using System.IO; | |
using System.Threading.Tasks; | |
using Azure.Storage.Blobs; | |
using Azure.Storage.Blobs.Models; | |
namespace AzureBlobDownload | |
{ | |
class AzureBlobDownload | |
{ | |
static async Task Main() | |
{ | |
string uriString = ConfigurationManager.AppSettings.Get("SasUri"); | |
string localDownloadPath = ConfigurationManager.AppSettings.Get("LocalDownloadPath"); | |
Uri sasUri = new Uri(uriString); | |
BlobContainerClient blobContainerClient = new BlobContainerClient(sasUri); | |
await foreach (BlobItem blob in blobContainerClient.GetBlobsAsync()) | |
{ | |
string fileName = blob.Name; | |
string localFilePath = Path.Combine(localDownloadPath, fileName); | |
using (var file = File.Open(localFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) | |
{ | |
var blobClient = blobContainerClient.GetBlobClient(blob.Name); | |
await blobClient.DownloadToAsync(file); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment