0% found this document useful (0 votes)
28 views

Retrieving Azure Storage Resources and Metadata by Using The Azure

This document provides steps to create Azure storage resources like accounts and containers, upload blobs using the Azure portal and .NET SDK, and retrieve blob metadata using the .NET SDK. It covers creating a storage account and containers, uploading files to containers via the portal, and enumerating and accessing blobs and containers using the Azure Storage SDK for .NET.

Uploaded by

saphana9800
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Retrieving Azure Storage Resources and Metadata by Using The Azure

This document provides steps to create Azure storage resources like accounts and containers, upload blobs using the Azure portal and .NET SDK, and retrieve blob metadata using the .NET SDK. It covers creating a storage account and containers, uploading files to containers via the portal, and enumerating and accessing blobs and containers using the Azure Storage SDK for .NET.

Uploaded by

saphana9800
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Retrieving Azure Storage resources

and metadata by using the Azure


Storage SDK for .NET
[Edition 03]

[Last Update 200915]

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

This activity guide cover steps to

• Create containers and upload blobs by using the Azure portal.

• Enumerate blobs and containers by using the Microsoft Azure Storage SDK for

.NET.

• Pull blob metadata by using the Storage SDK.

[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.

3.1 Create a Storage account


1. Sign-in to the the Azure portal (https://portal.azure.com ).

2. In the Azure portal's navigation pane, select All services.

3. From the All services blade, select Storage Accounts.

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,

Tags, and Review + Create.

7. Select the Basics tab, and in the tab area, perform the following actions:

i. Leave the Subscription text box set to its default value.

[email protected] 6
ii. In the Resource group section, select Create new, enter StorageMedia (or any

name), and then select OK.

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.

v. In the Performance section, select Standard.

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).

viii. In the Access tier section, ensure that Hot is selected.

ix. Select Review + Create.

[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.

9. In the Azure portal's navigation pane, select All services.

10. On the All services blade, select Storage Accounts.

11. On the Storage accounts blade, select the k21mediastor[yourname]** storage

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

Access keys link.

[email protected] 9
15. In the Access keys section, perform the following actions:

i. Record the value in the Storage account name text box.

ii. Select any one of the keys, and then record the value in either of the Key boxes.

3.2 Create storage account


containers
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 create two new

containers by selecting + Container with the following settings:

[email protected] 10
i. Name: raster-graphics

ii. Public access level: Private (no anonymous access)

iii. And then select Create.

iv. Then create a second new container with the following settings:

v. Name: compressed-audio

vi. Public access level: Private (no anonymous access)

vii. And then select Create.

[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

created raster-graphics container.

3. In the Files section, select the Folder icon.

[email protected] 13
4. In the File Explorer window, browse to the location where you downloaded the Github

Folder and go to \Allfiles\Labs\03\Starter\Images, in that folder and select the graph.jpg

file, and then select Open.

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

4.1 Create .NET project


1. On the Start screen, select the Visual Studio Code tile.

2. From the File menu, select Open Folder.

3. In the File Explorer window that opens, browse to the location where you downloaded the

Github Folder and go to \Allfiles\Labs\03\Starter\BlobManager, and then select Select

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

create a new .NET project named BlobManager in the current folder:

[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

name as the project.

6. Using the same terminal, import version 12.0.0 of Azure.Storage.Blobs from NuGet:

dotnet add package Azure.Storage.Blobs --version 12.0.0

[email protected] 18
Note: The dotnet add package command will add the Azure.Storage.Blobs package from

NuGet. For more information, go to Azure.Storage.Blobs.

7. Using the same terminal, build the .NET web application:

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.

2. Delete all existing code in the Program.cs file.

3. Add the following line of code to import the Azure.Storage, Azure.Storage.Blobs, and

Azure.Storage.Blobs.Models namespaces from the Azure.Storage.Blobs package

imported from NuGet:

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

be used in this file:

using System;

[email protected] 20
using System.Threading.Tasks;

5. Enter the following code to create a new Program class:

public class Program

6. In the Program class, enter the following line of code to create a new string constant

named blobServiceEndpoint:

private const string blobServiceEndpoint = "<primary-blob-service-endpoint>";

Note: Replace the <primary-blob-service-endpoint> string constant by setting its value

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:

private const string storageAccountName = "<storage-account-name>";

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:

private const string storageAccountKey = "<key>";

Note: Replace the <key> string constant by setting its value to the Key of the Storage

account that you recorded earlier in this lab.


[email protected] 22
9. In the Program class, enter the following code to create a new asynchronous Main

method:

public static async Task Main(string[] args)

10. Observe the Program.cs file, which should now include:

using Azure.Storage;

using Azure.Storage.Blobs;

[email protected] 23
using Azure.Storage.Blobs.Models;

using System;

using System.Threading.Tasks;

public class Program

private const string blobServiceEndpoint = "<primary-blob-service-endpoint>";

private const string storageAccountName = "<storage-account-name>";

private const string storageAccountKey = "<key>";

public static async Task Main(string[] args)

[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 class by using the storageAccountName and

storageAccountKey constants as constructor parameters:

StorageSharedKeyCredential accountCredentials = new

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

BlobServiceClient class by using the blobServiceEndpoint constant and the

accountCredentials variable as constructor parameters:

[email protected] 25
BlobServiceClient serviceClient = new BlobServiceClient(new

Uri(“<blobServiceEndpoint>”), accountCredentials);

Note: Replace <blobServiceEndpoint> constant with the URI of your blob which you recorded

earlier in the lab.

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:

AccountInfo info = await serviceClient.GetAccountInfoAsync();

4. In the Main method, add the following line of code to render a welcome message:

await Console.Out.WriteLineAsync($"Connected to Azure Storage Account");

[email protected] 26
5. In the Main method, add the following line of code to render the storage account's name:

await Console.Out.WriteLineAsync($"Account name:\t{storageAccountName}");

6. In the Main method, add the following line of code to render the type of storage account:

await Console.Out.WriteLineAsync($"Account kind:\t{info?.AccountKind}");

7. In the Main method, add the following line of code to render the currently selected stock

keeping unit (SKU) for the storage account:

await Console.Out.WriteLineAsync($"Account sku:\t{info?.SkuName}");

[email protected] 27
8. Observe the Main method, which should now include:

public static async Task Main(string[] args)

StorageSharedKeyCredential accountCredentials = new

StorageSharedKeyCredential(“storageAccountName”, “storageAccountKey”);

BlobServiceClient serviceClient = new BlobServiceClient(new

Uri(“blobServiceEndpoint”), accountCredentials);

AccountInfo info = await serviceClient.GetAccountInfoAsync();

await Console.Out.WriteLineAsync($"Connected to Azure Storage Account");

await Console.Out.WriteLineAsync($"Account name:\t{storageAccountName}");

await Console.Out.WriteLineAsync($"Account kind:\t{info?.AccountKind}");

await Console.Out.WriteLineAsync($"Account sku:\t{info?.SkuName}");

[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

the .NET web application:

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.

4.4 Enumerate the existing


containers
1. In the Program class, enter the following code to create a new private static method

named EnumerateContainersAsync that's asynchronous and has a single

BlobServiceClient parameter type:

private static async Task EnumerateContainersAsync(BlobServiceClient

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

GetBlobContainersAsync method of the BlobServiceClient class:

await foreach (BlobContainerItem container in client.GetBlobContainersAsync())

[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}");

4. Observe the EnumerateContainersAsync method, which should now include:

private static async Task EnumerateContainersAsync(BlobServiceClient client)

await foreach (BlobContainerItem container in client.GetBlobContainersAsync())

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

EnumerateContainersAsync method, passing in the serviceClient variable as a

parameter:

await EnumerateContainersAsync(serviceClient);

[email protected] 32
6. Observe the Main method, which should now include:

public static async Task Main(string[] args)

\\ Existing code removed for brevity

await EnumerateContainersAsync(serviceClient);

7. Save the Program.cs file.

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

the .NET web application:

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.

5.1 Enumerate the blobs in an


existing container by using
the SDK
1. In the Program class, enter the following code to create a new private static method

named EnumerateBlobsAsync that's asynchronous and has two parameter types,

BlobServiceClient and string:

private static async Task EnumerateBlobsAsync(BlobServiceClient client, string

containerName)

2. In the EnumerateBlobsAsync method, enter the following code to get a new instance of

the BlobContainerClient class by using the GetBlobContainerClient method of the

BlobServiceClient class, passing in the containerName parameter:


[email protected] 35
BlobContainerClient container = client.GetBlobContainerClient(containerName);

3. In the EnumerateBlobsAsync method, enter the following code to render the name of the

container that will be enumerated:

await Console.Out.WriteLineAsync($"Searching:\t{container.Name}");

4. In the EnumerateBlobsAsync method, enter the following code to create an

asynchronous foreach loop that iterates over the results of an invocation of the

GetBlobsAsync method of the BlobContainerClient class:

await foreach (BlobItem blob in container.GetBlobsAsync())

[email protected] 36
5. Within the foreach loop, enter the following code to print the name of each blob:

await Console.Out.WriteLineAsync($"Existing Blob:\t{blob.Name}");

6. Observe the EnumerateBlobsAsync method, which should now include:

private static async Task EnumerateBlobsAsync(BlobServiceClient client, string

containerName)

BlobContainerClient container = client.GetBlobContainerClient(containerName);

await Console.Out.WriteLineAsync($"Searching:\t{container.Name}");

await foreach (BlobItem blob in container.GetBlobsAsync())

[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

named existingContainerName with a value of raster-graphics:

string existingContainerName = "raster-graphics";

8. In the Main method, enter the following code at the end of the method to invoke the

EnumerateBlobsAsync method, passing in the serviceClient and existingContainerName

variables as parameters:

await EnumerateBlobsAsync(serviceClient, existingContainerName);

[email protected] 38
9. Observe the Main method, which should now include:

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

the .NET web application:

dotnet run

13. Observe the output from the currently running console application. The updated output

includes metadata about the existing container and blobs.

14. Select Kill Terminal or the Recycle Bin icon to close the currently open terminal and any

associated processes.

5.2 Create a new container by


using the SDK
1. In the Program class, create a new private static method named GetContainerAsync

that's asynchronous and has two parameter types, BlobServiceClient and string:

[email protected] 39
private static async Task<BlobContainerClient>

GetContainerAsync(BlobServiceClient client, string containerName)

2. In the GetContainerAsync method, enter the following code to get a new instance of the

BlobContainerClient class by using the GetBlobContainerClient method of the

BlobServiceClient class, passing in the containerName parameter:

BlobContainerClient container = client.GetBlobContainerClient(containerName);

[email protected] 40
3. In the GetContainerAsync method, enter the following code to invoke the

CreateIfNotExistsAsync method of the BlobContainerClient class:

await container.CreateIfNotExistsAsync(PublicAccessType.Blob);

4. In the GetContainerAsync method, enter the following code to render the name of the

container that was potentially created:

await Console.Out.WriteLineAsync($"New Container:\t{container.Name}");

5. In the GetContainerAsync method, enter the following code to return the instance of the

BlobContainerClient class named container as the result of the GetContainerAsync

method:

return container;

[email protected] 41
6. Observe the GetContainerAsync method, which should now include:

private static async Task<BlobContainerClient>

GetContainerAsync(BlobServiceClient client, string containerName)

BlobContainerClient container = client.GetBlobContainerClient(containerName);

await container.CreateIfNotExistsAsync(PublicAccessType.Blob);

await Console.Out.WriteLineAsync($"New Container:\t{container.Name}");

return container;

7. In the Main method, enter the following code at the end of the method to create a variable

named newContainerName with a value of vector-graphics:

string newContainerName = "vector-graphics";

[email protected] 42
8. In the Main method, enter the following code at the end of the method to invoke the

GetContainerAsync method, passing in the serviceClient and newContainerName

variables as parameters, and to store the result in a variable named containerClient of

type BlobContainerClient:

BlobContainerClient containerClient = await GetContainerAsync(serviceClient,

newContainerName);

9. Observe the Main method, which should now include:

public static async Task Main(string[] args)

\\ Existing code removed for brevity

await EnumerateContainersAsync(serviceClient);

[email protected] 43
string existingContainerName = "raster-graphics";

await EnumerateBlobsAsync(serviceClient, existingContainerName);

string newContainerName = "vector-graphics";

BlobContainerClient containerClient = await GetContainerAsync(serviceClient,

newContainerName);

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

the .NET web application:

dotnet run

13. Observe the output from the currently running console application. The updated output

includes metadata about the existing container and blobs.

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

that you created earlier in this lab.

4. On the Storage account blade, select the Containers link in the Blob service section.

5. In the Containers section, select the newly created vector-graphics container.

6. On the Container blade, select Upload.

7. In the Upload blob window, perform the following actions:

i. In the Files section, select the Folder icon.

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

named GetBlobAsync that's asynchronous and has two parameter types,

BlobContainerClient and string:

private static async Task<BlobClient> GetBlobAsync(BlobContainerClient client,

string blobName)

[email protected] 46
2. In the GetBlobAsync method, enter the following code to get a new instance of the

BlobClient class by using the GetBlobClient method of the BlobContainerClient class,

passing in the blobName parameter:

BlobClient blob = client.GetBlobClient(blobName);

3. In the GetBlobAsync method, enter the following code to render the name of the blob that

was referenced:

await Console.Out.WriteLineAsync($"Blob Found:\t{blob.Name}");

4. In the GetBlobAsync method, enter the following code to return the instance of the

BlobClient class named blob as the result of the GetBlobAsync method:

return blob;

[email protected] 47
5. Observe the GetBlobAsync method, which should now include:

private static async Task<BlobClient> GetBlobAsync(BlobContainerClient client,

string blobName)

BlobClient blob = client.GetBlobClient(blobName);

await Console.Out.WriteLineAsync($"Blob Found:\t{blob.Name}");

return blob;

6. In the Main method, enter the following code at the end of the method to create a variable

named uploadedBlobName with a value of graph.svg:

string uploadedBlobName = "graph.svg";

[email protected] 48
7. In the Main method, enter the following code at the end of the method to invoke the

GetBlobAsync method, passing in the containerClient and uploadedBlobName variables

as parameters, and to store the result in a variable named blobClient of type BlobClient:

BlobClient blobClient = await GetBlobAsync(containerClient, uploadedBlobName);

8. In the Main method, enter the following code at the end of the method to render the Uri

property of the blobClient variable:

await Console.Out.WriteLineAsync($"Blob Url:\t{blobClient.Uri}");

[email protected] 49
9. Observe the Main method, which should now include:

public static async Task Main(string[] args)

\\ Existing code removed for brevity

await EnumerateContainersAsync(serviceClient);

string existingContainerName = "raster-graphics";

await EnumerateBlobsAsync(serviceClient, existingContainerName);

string newContainerName = "vector-graphics";

BlobContainerClient containerClient = await GetContainerAsync(serviceClient,

newContainerName);

string uploadedBlobName = "graph.svg";

BlobClient blobClient = await GetBlobAsync(containerClient, uploadedBlobName);

await Console.Out.WriteLineAsync($"Blob Url:\t{blobClient.Uri}");

}
[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

the .NET web application:

dotnet run

13. Observe the output from the currently running console application. The updated output

includes metadata about the existing container and blobs.

Note: The URL will likely be similar to the following string:

https://k21mediastor*[yourname]*.blob.core.windows.net/vector-graphics/graph.svg

5.5 Test the URI by using a


browser
1. Copy the Blob URL from the last output result of the Visual Studio Code window.

2. In the new browser window, paste the copied blob URL.

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.

[email protected] 53

You might also like