Lab 4: Retrieve blob Uniform Resource Identifiers (URIs) by using the .NET SDK

Task 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:

    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())
    {        
    }
    
  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. Review 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())
        {        
             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);
    
  9. Observe the Program.cs file, which should now include:

    using Azure.Storage;
    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    using System;
    using System.Threading.Tasks;    
    public class Program
    {
        private const string blobServiceEndpoint = "your blobServiceEndpoint";
        private const string storageAccountName = "your storageAccountName";
        private const string storageAccountKey = "your storageAccountKey";    
        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}");
            await EnumerateContainersAsync(serviceClient);
            string existingContainerName = "raster-graphics";
            await EnumerateBlobsAsync(serviceClient, existingContainerName);
        }        
        private static async Task EnumerateContainersAsync(BlobServiceClient client)
        {        
            await foreach (BlobContainerItem container in client.GetBlobContainersAsync())
            {
                await Console.Out.WriteLineAsync($"Container:\t{container.Name}");
            }
        }        
        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())
            {        
                await Console.Out.WriteLineAsync($"Existing Blob:\t{blob.Name}");
            }
        }
    }
    
  10. Save the Program.cs file.

  11. In the Visual Studio Code window, on the Menu Bar, select Terminal and then select New Terminal.

  12. In the terminal, run the following command to run the .NET web application:

    dotnet run
    

    Note: If there are any build errors, review the Program.cs file in the $HOME\training-az204\\Labs\03\Solution\BlobManager folder.

  13. Review 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.

Task 2: Create a new container by using the SDK

  1. In the Program class, enter the following code to create a new private static method named GetContainerAsync that's asynchronous and has two parameter types, BlobServiceClient and string:

    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);
    
  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;
    
  6. Review 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";
    
  8. In the Main method, enter the following code at the end of the method to invoke the GetContainerAsync method, to pass 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. Review the Program.cs file, which should now include:

    using Azure.Storage;
    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    using System;
    using System.Threading.Tasks;    
    public class Program
    {
        private const string blobServiceEndpoint = "your blobServiceEndpoint";
        private const string storageAccountName = "your storageAccountName";
        private const string storageAccountKey = "your storageAccountKey";
        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}");
            await EnumerateContainersAsync(serviceClient);
            string existingContainerName = "raster-graphics";
            await EnumerateBlobsAsync(serviceClient, existingContainerName);
            string newContainerName = "vector-graphics";
            BlobContainerClient containerClient = await GetContainerAsync(serviceClient, newContainerName);
        }        
        private static async Task EnumerateContainersAsync(BlobServiceClient client)
        {        
            await foreach (BlobContainerItem container in client.GetBlobContainersAsync())
            {
                await Console.Out.WriteLineAsync($"Container:\t{container.Name}");
            }
        }        
        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())
            {        
                await Console.Out.WriteLineAsync($"Existing Blob:\t{blob.Name}");
            }
        }        
        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;
        }
    }
    
  10. Save the Program.cs file.

  11. In the Visual Studio Code window, on the Menu Bar, select Terminal and then select New Terminal.

  12. In the terminal, run the following command to run the .NET web application:

    dotnet run
    

    Note: If there are any build errors, review the Program.cs file in the $HOME\training-az204\Labs\03\Solution\BlobManager folder.

  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.

Task 3: Upload a new blob by using the portal

  1. On the Azure portal's navigation pane, select the Resource groups link.

  2. On the Resource groups blade, select the StorageMedia resource group that you created previously in this lab.

  3. On the StorageMedia blade, select the mediastor[yourname] storage account that you created previously in this lab.

  4. On the Storage account blade, select the Containers link in the Data storage section.

  5. In the Containers section, select the newly created vector-graphics container. You might need to refresh the page to observe the new container.

  6. On the Container blade, select Upload.

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

    | Setting | Action | | -- | -- | | Files section Select the Folder icon | | File Explorer window | $HOME\training-az204\Labs\03\Starter\Images, select the graph.svg file, and then select Open | | Overwrite if files already exist check box | Ensure that the check box is selected |

    Note: Wait for the blob to upload before you continue with this lab.

Task 4: Access blob URI by using the SDK

  1. Switch to the Visual Studio Code window.

  2. 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)
    {      
    }
    
  3. 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, and to pass in the blobName parameter:

    BlobClient blob = client.GetBlobClient(blobName);
    
  4. 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}");
    
  5. 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;
    
  6. Review 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;
    }
    
  7. 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";
    
  8. 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);
    
  9. 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}");
    
  10. Observe the Program.cs file, which should now include:

    using Azure.Storage;
    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    using System;
    using System.Threading.Tasks;    
    public class Program
    {
        private const string blobServiceEndpoint = "your blobServiceEndpoint";
        private const string storageAccountName = "your storageAccountName";
        private const string storageAccountKey = "your storageAccountKey";    
        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}");
            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}");
        }        
        private static async Task EnumerateContainersAsync(BlobServiceClient client)
        {        
            await foreach (BlobContainerItem container in client.GetBlobContainersAsync())
            {
                await Console.Out.WriteLineAsync($"Container:\t{container.Name}");
            }
        }        
        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())
            {        
                await Console.Out.WriteLineAsync($"Existing Blob:\t{blob.Name}");
            }
        }        
        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;
        }        
        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;
        }
    }
    
  11. Save the Program.cs file.

  12. In the Visual Studio Code window, activate the shortcut menu for the Explorer pane, and then select Open in Integrated Terminal.

  13. At the open command prompt, run the following command to run the .NET web application:

    dotnet run
    

    Note: If there are any build errors, review the Program.cs file in the $HOME\training-az204\Labs\03\Solution\BlobManager folder.

  14. Observe the output from the currently running console application. The updated output includes the final URL to access the blob online. Record the value of this URL to use later in the lab.

    Note: The URL will likely be similar to the following string: https://mediastor*[yourname]*.blob.core.windows.net/vector-graphics/graph.svg

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

Task 5: Test the URI by using a browser

  1. On the taskbar, activate the shortcut menu for the Microsoft Edge icon, and then select New window.

  2. In the new browser window, refer to the URL that you previously copied in this lab for the blob.

  3. You should now notice the Scalable Vector Graphics (SVG) file in your browser window.

Review

In this exercise, you created containers and managed blobs by using the Storage SDK.

results matching ""

    No results matching ""