Retrieving Azure Storage Resources and Metadata by Using The Azure
Retrieving Azure Storage Resources and Metadata by Using The Azure
Atul Kumar
Oracle ACE & Cloud Expert
[email protected] 1
Contents
1 Introduction .........................................................................................................................................................3
2 Documentation Links ........................................................................................................................................4
3 Create Azure resources ....................................................................................................................................5
3.1 Create a Storage account .......................................................................................................................................... 5
3.2 Create storage account containers ..................................................................................................................... 10
3.3 Upload a storage account blob ............................................................................................................................. 13
3.3.1 Pre-requisite .........................................................................................................................................................................13
4 Access containers by using the .NET SDK ................................................................................................ 16
4.1 Create .NET project................................................................................................................................................... 16
4.2 Modify the Program class to access Storage.................................................................................................... 20
4.3 Connect to the Azure Storage blob service endpoint ................................................................................... 25
4.4 Enumerate the existing containers .................................................................................................................... 30
5 Retrieve blob Uniform Resource Identifiers (URIs) by using the .NET SDK ............................... 35
5.1 Enumerate the blobs in an existing container by using the SDK ............................................................. 35
5.2 Create a new container by using the SDK ......................................................................................................... 39
5.3 Upload a new blob by using the portal .............................................................................................................. 45
5.4 Access blob URI by using the SDK ....................................................................................................................... 46
5.5 Test the URI by using a browser .......................................................................................................................... 51
6 Summary............................................................................................................................................................. 53
[email protected] 2
1 INTRODUCTION
• Enumerate blobs and containers by using the Microsoft Azure Storage SDK for
.NET.
[email protected] 3
2 DOCUMENTATION LINKS
1. Manage Blob properties and metadata with .NET: https://docs.microsoft.com/en-
us/azure/storage/blobs/storage-blob-properties-metadata?tabs=dotnet
2. List blobs with .Net: https://docs.microsoft.com/en-us/azure/storage/blobs/storage-
blobs-list?tabs=dotnet
[email protected] 4
3 CREATE AZURE RESOURCES
In this section, we will create a new Storage account to use throughout the remainder of the lab.
4. From the Storage accounts blade, get your list of storage account instances.
[email protected] 5
5. From the Storage accounts blade, select Add.
6. From the Create storage account blade, observe the tabs from the blade, such as Basics,
7. Select the Basics tab, and in the tab area, perform the following actions:
[email protected] 6
ii. In the Resource group section, select Create new, enter StorageMedia (or any
iii. In the Storage account name text box, enter k21mediastor (or any name).
iv. In the Location drop-down list, select the (US) East US region.
vi. In the Account kind drop-down list, select StorageV2 (general purpose v2).
vii. In the Replication drop-down list, select Read-access geo-redundant storage (RA-
GRS).
[email protected] 7
8. Select Create to create the storage account by using your specified configuration.
Note: Wait for the creation task to complete before you move forward with this lab.
account instance.
12. On the Storage account blade, find the Settings section, and then select the
Properties link.
13. In the Properties section, record the value of the Primary Blob Service Endpoint text
box.
[email protected] 8
Note: You'll use this value later in the lab.
14. Still on the Storage account blade, find the Settings section, and then select the
[email protected] 9
15. In the Access keys section, perform the following actions:
ii. Select any one of the keys, and then record the value in either of the Key boxes.
lab.
2. Select the Containers link in the Blob service section, and then create two new
[email protected] 10
i. Name: raster-graphics
iv. Then create a second new container with the following settings:
v. Name: compressed-audio
[email protected] 11
3. Back in the Containers section, observe the updated list of containers.
[email protected] 12
3.3 Upload a storage account
blob
3.3.1 PRE-REQUISITE
i. Download this Github code from here, which we are going to use in our lab.
1. Access the k21mediastor[yourname]** storage account that you created earlier in this
lab.
2. Select the Containers link in the Blob service section, and then select the recently
[email protected] 13
4. In the File Explorer window, browse to the location where you downloaded the Github
5. Ensure that the Overwrite if files already exist check box is selected, and then select
Upload.
[email protected] 14
[email protected] 15
4 ACCESS CONTAINERS BY USING THE .NET SDK
3. In the File Explorer window that opens, browse to the location where you downloaded the
Folder.
[email protected] 16
4. In the Visual Studio Code window, at the top select Terminal, and then select New
Terminal.
5. At the open command prompt, enter the following command, and then select Enter to
[email protected] 17
dotnet new console --name BlobManager --output .
Note: The dotnet new command will create a new console project in a folder with the same
6. Using the same terminal, import version 12.0.0 of Azure.Storage.Blobs from NuGet:
[email protected] 18
Note: The dotnet add package command will add the Azure.Storage.Blobs package from
dotnet build
8. Select Kill Terminal or the Recycle Bin icon to close the currently open terminal and any
associated processes.
[email protected] 19
4.2 Modify the Program class to
access Storage
1. In the Explorer pane of the Visual Studio Code window, open the Program.cs file.
3. Add the following line of code to import the Azure.Storage, Azure.Storage.Blobs, and
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
4. Add the following lines of code to add using directives for the built-in namespaces that will
using System;
[email protected] 20
using System.Threading.Tasks;
6. In the Program class, enter the following line of code to create a new string constant
named blobServiceEndpoint:
to the Primary Blob Service Endpoint of the Storage account that you recorded earlier in
this lab.
[email protected] 21
7. In the Program class, enter the following line of code to create a new string constant
named storageAccountName:
Note: Replace the <storage-account-name> string constant by setting its value to the
Storage account name of the Storage account that you recorded earlier in this lab.
8. In the Program class, enter the following line of code to create a new string constant
named storageAccountKey:
Note: Replace the <key> string constant by setting its value to the Key of the Storage
method:
using Azure.Storage;
using Azure.Storage.Blobs;
[email protected] 23
using Azure.Storage.Blobs.Models;
using System;
using System.Threading.Tasks;
[email protected] 24
4.3 Connect to the Azure Storage
blob service endpoint
1. In the Main method, add the following line of code to create a new instance of the
StorageSharedKeyCredential(“<storageAccountName>”, “<storageAccountKey>”);
Note: Replace the <storageAccountName> constant string with your Storage Account
name text value and <storageAccountKey> with the Key you recorded before in the lab.
2. In the Main method, add the following line of code to create a new instance of the
[email protected] 25
BlobServiceClient serviceClient = new BlobServiceClient(new
Uri(“<blobServiceEndpoint>”), accountCredentials);
Note: Replace <blobServiceEndpoint> constant with the URI of your blob which you recorded
3. In the Main method, add the following line of code to invoke the GetAccountInfoAsync
method of the BlobServiceClient class to retrieve account metadata from the service:
4. In the Main method, add the following line of code to render a welcome message:
[email protected] 26
5. In the Main method, add the following line of code to render the storage account's name:
6. In the Main method, add the following line of code to render the type of storage account:
7. In the Main method, add the following line of code to render the currently selected stock
[email protected] 27
8. Observe the Main method, which should now include:
StorageSharedKeyCredential(“storageAccountName”, “storageAccountKey”);
Uri(“blobServiceEndpoint”), accountCredentials);
[email protected] 28
9. Save the Program.cs file by using Ctrl+S.
10. In the Visual Studio Code window and then select New Terminal.
11. At the open command prompt, enter the following command, and then select Enter to run
dotnet run
Note: If there are any build errors, review the Program.cs file
12. Observe the output from the currently running console application. The output contains
metadata for the Storage account that was retrieved from the service.
[email protected] 29
13. Select Kill Terminal or the Recycle Bin icon to close the currently open terminal and any
associated processes.
client)
[email protected] 30
2. In the EnumerateContainersAsync method, enter the following code to create an
asynchronous foreach loop that iterates over the results of an invocation of the
[email protected] 31
3. Within the foreach loop, enter the following code to print the name of each container:
await Console.Out.WriteLineAsync($"Container:\t{container.Name}");
await Console.Out.WriteLineAsync($"Container:\t{container.Name}");
5. In the Main method, enter the following code at the end of the method to invoke the
parameter:
await EnumerateContainersAsync(serviceClient);
[email protected] 32
6. Observe the Main method, which should now include:
await EnumerateContainersAsync(serviceClient);
8. In the Visual Studio Code window and then select New Terminal.
9. At the open command prompt, enter the following command, and then select Enter to run
dotnet run
10. Observe the output from the currently running console application. The output contains
metadata for the Storage account that was retrieved from the service.
[email protected] 33
11. Select Kill Terminal or the Recycle Bin icon to close the currently open terminal and any
associated processes.
[email protected] 34
5 RETRIEVE BLOB UNIFORM RESOURCE IDENTIFIERS (URIS) BY
USING THE .NET SDK
In this section, we will create containers and manage blobs by using the Storage SDK.
containerName)
2. In the EnumerateBlobsAsync method, enter the following code to get a new instance of
3. In the EnumerateBlobsAsync method, enter the following code to render the name of the
await Console.Out.WriteLineAsync($"Searching:\t{container.Name}");
asynchronous foreach loop that iterates over the results of an invocation of the
[email protected] 36
5. Within the foreach loop, enter the following code to print the name of each blob:
containerName)
await Console.Out.WriteLineAsync($"Searching:\t{container.Name}");
[email protected] 37
await Console.Out.WriteLineAsync($"Existing Blob:\t{blob.Name}");
7. In the Main method, enter the following code at the end of the method to create a variable
8. In the Main method, enter the following code at the end of the method to invoke the
variables as parameters:
[email protected] 38
9. Observe the Main method, which should now include:
11. In the Visual Studio Code window and then select New Terminal.
12. At the open command prompt, enter the following command, and then select Enter to run
dotnet run
13. Observe the output from the currently running console application. The updated output
14. Select Kill Terminal or the Recycle Bin icon to close the currently open terminal and any
associated processes.
that's asynchronous and has two parameter types, BlobServiceClient and string:
[email protected] 39
private static async Task<BlobContainerClient>
2. In the GetContainerAsync method, enter the following code to get a new instance of the
[email protected] 40
3. In the GetContainerAsync method, enter the following code to invoke the
await container.CreateIfNotExistsAsync(PublicAccessType.Blob);
4. In the GetContainerAsync method, enter the following code to render the name of the
5. In the GetContainerAsync method, enter the following code to return the instance of the
method:
return container;
[email protected] 41
6. Observe the GetContainerAsync method, which should now include:
await container.CreateIfNotExistsAsync(PublicAccessType.Blob);
return container;
7. In the Main method, enter the following code at the end of the method to create a variable
[email protected] 42
8. In the Main method, enter the following code at the end of the method to invoke the
type BlobContainerClient:
newContainerName);
await EnumerateContainersAsync(serviceClient);
[email protected] 43
string existingContainerName = "raster-graphics";
newContainerName);
11. In the Visual Studio Code window and then select New Terminal.
12. At the open command prompt, enter the following command, and then select Enter to run
dotnet run
13. Observe the output from the currently running console application. The updated output
14. Select Kill Terminal or the Recycle Bin icon to close the currently open terminal and any
associated processes.
[email protected] 44
5.3 Upload a new blob by using
the portal
1. In the Azure portal's navigation pane, select the Resource groups link.
2. On the Resource groups blade, find and then select the StorageMedia [your resource
group name] resource group that you created earlier in this lab.
3. On the StorageMedia [your RG] blade, select the mediastor [yourname]** storage account
4. On the Storage account blade, select the Containers link in the Blob service section.
ii. In the File Explorer window, browse to the location where you downloaded the folder from
Github \Allfiles\Labs\03\Starter\Images, select the graph.svg file, and then select Open.
iii. Ensure that the Overwrite if files already exist check box is selected, and then select
Upload.
[email protected] 45
5.4 Access blob URI by using the
SDK
1. In the Program class, enter the following code to create a new private static method
string blobName)
[email protected] 46
2. In the GetBlobAsync method, enter the following code to get a new instance of the
3. In the GetBlobAsync method, enter the following code to render the name of the blob that
was referenced:
4. In the GetBlobAsync method, enter the following code to return the instance of the
return blob;
[email protected] 47
5. Observe the GetBlobAsync method, which should now include:
string blobName)
return blob;
6. In the Main method, enter the following code at the end of the method to create a variable
[email protected] 48
7. In the Main method, enter the following code at the end of the method to invoke the
as parameters, and to store the result in a variable named blobClient of type BlobClient:
8. In the Main method, enter the following code at the end of the method to render the Uri
[email protected] 49
9. Observe the Main method, which should now include:
await EnumerateContainersAsync(serviceClient);
newContainerName);
}
[email protected] 50
10. Save the Program.cs file.
11. In the Visual Studio Code window and then select New Terminal.
12. At the open command prompt, enter the following command, and then select Enter to run
dotnet run
13. Observe the output from the currently running console application. The updated output
https://k21mediastor*[yourname]*.blob.core.windows.net/vector-graphics/graph.svg
3. You should now notice the Scalable Vector Graphic (SVG) file in your browser window.
[email protected] 51
[email protected] 52
6 SUMMARY
This completes step by step guide covering:
1. How to Create Container and upload Blob using Azure Portal.
2. How to Access containers by using the .NET SDK
3. How to Access the blobs in an existing container by using the .NET SDK
4. How to Create a new container by using the .NET SDK
5. How to Access blob URI by using the SDK
6. Test the URI by using a browser.