Access Files on Public Azure BLOB Storage using .NET Core

Hendi Suhardja
2 min readJun 3, 2020

--

Azure Blob storage is Microsoft’s object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data, such as text or binary data.

Blob storage is ideal for:

  • Serving images or documents directly to a browser.
  • Storing files for distributed access.
  • Streaming video and audio.
  • Storing data for backup and restore, disaster recovery, and archiving.
  • Storing data for analysis by an on-premises or Azure-hosted service.

Objects in Blob storage can be accessed from anywhere in the world via HTTP or HTTPS. Users or client applications can access blobs via URLs, the Azure Storage REST API, Azure PowerShell, Azure CLI, or an Azure Storage client library.

Azure Files enables you to set up highly available network file shares that can be accessed by using the standard Server Message Block (SMB) protocol. That means that multiple VMs can share the same files with both read and write access. You can also read the files using the REST interface or the storage client libraries.

One thing that distinguishes Azure Files from files on a corporate file share is that you can access the files from anywhere in the world using a URL that points to the file and includes a shared access signature (SAS) token. You can generate SAS tokens; they allow specific access to a private asset for a specific amount of time.

Access Public Azure Blob Storage

Let’s say we have public azure blob storage url “https://{accountName}.blob.core.windows.net/{containerName}”, which could be accessed without any credential, we could access the files using .NET Core

  • First you need to open your package manager, search “Microsoft.WindowsAzure.Storage”, “Microsoft.WindowsAzure.Storage.Auth” and “Microsoft.WindowsAzure.Storage.Blob” then click install
  • Create new project , e.g : Console project, then write below code

Note : change {accountName} with your azure storage account name, change {containerName} to your azure storage container name, change {maxRecordNumber} to your maximum number records

CloudStorageAccount mycloudStorageAccount = new CloudStorageAccount(new StorageCredentials(), {accountName}, "", true);
CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference({containerName});
var blobList = await container.ListBlobsSegmentedAsync(string.Empty, false, BlobListingDetails.None, {maxRecordNumber}, null, null, null);

So let’s say we want to download 1000 files inside azure blob storage, we could do that by below code

CloudStorageAccount mycloudStorageAccount = new CloudStorageAccount(new StorageCredentials(), {accountName}, "", true);
CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference({containerName});var blobList = await container.ListBlobsSegmentedAsync(string.Empty, false, BlobListingDetails.None, 1000, null, null, null);foreach (CloudBlockBlob item in blobList.Results){await item.DownloadToFileAsync($"{item.Name}", FileMode.CreateNew);}

In case you only need to write the content of the file without downloading it, you could use below code

CloudStorageAccount mycloudStorageAccount = new CloudStorageAccount(new StorageCredentials(), {accountName}, "", true);
CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference({containerName});var blobList = await container.ListBlobsSegmentedAsync(string.Empty, false, BlobListingDetails.None, 1000, null, null, null);foreach (CloudBlockBlob item in blobList.Results){string fileText = await item.DownloadTextAsync();}

Thanks for reading

--

--