Azure Blob Storage Exercises

Exercise 1 - Azure portal

Prerequisites:

  • Azure subscription - link
  • Azure storage accoung - link

Task:

  • Create container and upload any image.
  • Open uploaded image through web browser.
Solution
  • Go to Azure Portal.
  • Go to Storage Explorer.
  • Select storage account you created.
  • Right click on Blob containers menu item. Thank select Create blob container option.
  • Enter name e.g. images. Select public access level (Blob is enough for this exercise).
  • Refresh view. Select created container. Click upload and upload an image.
  • Select uploaded blob. Click Copy URL button.
  • Paste url in web browser. Look at you wonderful blob :)


Exercise 2 - Connection string

Prerequisites:

  • Exercise 1

Task:

  • From Azure Portal copy you connection string.
  • Paste it into the settings.json file as a value for the "StorageConnectionString" key.
Solution
  • Go to Azure Portal.
  • Go to Storage accounts. Select you storage account.
  • Click the "Access keys" menu item in "Security + networking section".
  • Click show keys.
  • Cope key1 Connection string value.
  • Paste copied string into the settings.json file as a value for the "StorageConnectionString" key.


Exercise 3 - Upload blob

Prerequisites:

  • Exercise 2

Task:

  • Implement missing part of the UploadAsync method in the AzureBlobService.cs file

Useful links:

Solution
var blob = blobContainer.GetBlockBlobReference(file.FileName);
using (var stream = file.OpenReadStream())
{
    await blob.UploadFromStreamAsync(stream);
}


Exercise 4 - List blobs

Prerequisites:

  • Exercise 2

Task:

  • Implement missing part of the ListAsync method in the AzureBlobService.cs file

Useful links:

Solution
BlobContinuationToken blobContinuationToken = null;
do
{
	var response = await blobContainer.ListBlobsSegmentedAsync(blobContinuationToken);
	foreach (IListBlobItem blob in response.Results)
    {
	    if (blob.GetType() == typeof(CloudBlockBlob))
			allBlobs.Add(blob.Uri);
	}
	blobContinuationToken = response.ContinuationToken;
} while (blobContinuationToken != null);


Exercise 5 - Delete blob

Prerequisites:

  • Exercise 2

Task:

  • Implement missing part of the DeleteAsync method in the AzureBlobService.cs file

Useful links:

Solution
Uri uri = new Uri(fileUri);
string filename = Path.GetFileName(uri.LocalPath);

var blob = blobContainer.GetBlockBlobReference(filename);
await blob.DeleteIfExistsAsync();


Exercise 5 - Delete all blobs

Prerequisites:

  • Exercise 2

Task:

  • Implement missing part of the DeleteAllAsync method in the AzureBlobService.cs file

Useful links:

Solution
BlobContinuationToken blobContinuationToken = null;
do
{
	var response = await blobContainer.ListBlobsSegmentedAsync(blobContinuationToken);
	foreach (IListBlobItem blob in response.Results)
	{
		if (blob.GetType() == typeof(CloudBlockBlob))
			await((CloudBlockBlob)blob).DeleteIfExistsAsync();
	}
	blobContinuationToken = response.ContinuationToken;
} while (blobContinuationToken != null);

Run the application

  • Go to AzureBlobLearning/AzureBlobLearning/AzureBlobLearning.
  • Run dotnet build.
  • Run dotnet run.
  • In web browser open the https://localhost:5001