Step 1: Upload image data in the cloud with Azure Storage

This tutorial is part one of a series. In this tutorial, you learn how to deploy a web app. The web app uses the Azure Blob Storage client library to upload images to a storage account.

In part one of the series, you do the following tasks:

  • Create a storage account
  • Create a container and set permissions
  • Retrieve an access key
  • Deploy a web app to Azure
  • Configure app settings
  • Interact with the web app


To complete this tutorial, you need an Azure subscription. Create a free account before you begin.

Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.

To start Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code or command block. Selecting Try It doesn't automatically copy the code or command to Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Go to, or select the Launch Cloud Shell button to open Cloud Shell in your browser. Screenshot that shows how to launch Cloud Shell in a new window.
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Screenshot that shows the Cloud Shell button in the Azure portal

To use Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block (or command block) to copy the code or command.

  3. Paste the code or command into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code or command.

Create a resource group


In step 2 of the tutorial, you use Azure Event Grid with the blob storage you create in this step. Create your storage account in an Azure region that supports Event Grid. For a list of supported regions, see Azure products by region.

  1. In the Azure Cloud Shell, select Bash in the top-left corner if it's not already selected.

    Screenshot showing the Azure Cloud Shell with the Bash option selected.

  2. Create a resource group with the az group create command. An Azure resource group is a logical container into which Azure resources are deployed and managed.


    Set appropriate values for region and rgName (resource group name).

    az group create --name $rgName --location $region

Create a storage account

The sample uploads images to a blob container in an Azure storage account.

Create a storage account in the resource group you created by using the az storage account create command.


az storage account create --name $blobStorageAccount --location $region \
  --resource-group $rgName --sku Standard_LRS --kind StorageV2 --access-tier hot --allow-blob-public-access true

Create Blob storage containers

The app uses two containers in the Blob storage account. The images container is where the app uploads full-resolution images. In the second step of the series, an Azure function app uploads resized image thumbnails to the thumbnails container.

The images container's public access is set to off. The thumbnails container's public access is set to container. The container public access setting permits users who visit the web page to view the thumbnails.

Get the storage account key by using the az storage account keys list command. Then, use this key to create two containers with the az storage container create command.

blobStorageAccountKey=$(az storage account keys list -g $rgName \
  -n $blobStorageAccount --query "[0].value" --output tsv)

az storage container create --name images \
  --account-name $blobStorageAccount \
  --account-key $blobStorageAccountKey

az storage container create --name thumbnails \
  --account-name $blobStorageAccount \
  --account-key $blobStorageAccountKey --public-access container

The sample app connects to the storage account using its name and access key.

Create an App Service plan

An App Service plan specifies the location, size, and features of the web server farm that hosts your app. The following example creates an App Service plan named myAppServicePlan in the Free pricing tier:

Create an App Service plan with the az appservice plan create command.

az appservice plan create --name $planName --resource-group $rgName --sku Free

Create a web app

The web app provides a hosting space for the sample app code that's deployed from the GitHub sample repository.

Create a web app in the myAppServicePlan App Service plan with the az webapp create command.


az webapp create --name $webapp --resource-group $rgName --plan $planName

Deploy the sample app from the GitHub repository

App Service supports several ways to deploy content to a web app. In this tutorial, you deploy the web app from a public GitHub sample repository. Configure GitHub deployment to the web app with the az webapp deployment source config command.

The sample project contains an ASP.NET MVC app. The app accepts an image, saves it to a storage account, and displays images from a thumbnail container. The web app uses the Azure.Storage, Azure.Storage.Blobs, and Azure.Storage.Blobs.Models namespaces to interact with the Azure Storage service.

az webapp deployment source config --name $webapp --resource-group $rgName \
  --branch master --manual-integration \

Configure web app settings

The sample web app uses the Azure Storage APIs for .NET to upload images. Storage account credentials are set in the app settings for the web app. Add app settings to the deployed app with the az webapp config appsettings set or New-AzStaticWebAppSetting command.

az webapp config appsettings set --name $webapp --resource-group $rgName \
  --settings AzureStorageConfig__AccountName=$blobStorageAccount \
    AzureStorageConfig__ImageContainer=images \
    AzureStorageConfig__ThumbnailContainer=thumbnails \

After you deploy and configure the web app, you can test the image upload functionality in the app.

Upload an image

To test the web app, browse to the URL of your published app. The default URL of the web app is https://<web_app> Then, select the Upload photos region to specify and upload a file, or drag a file onto the region. The image disappears if successfully uploaded. The Generated Thumbnails section remains empty until we test it later in this tutorial.


Run the following command to get the name of the web app: echo $webapp

Screenshot of the page to upload photos in the Image Resizer .NET app.

In the sample code, the UploadFileToStorage task in the Storagehelper.cs file is used to upload the images to the images container within the storage account using the UploadAsync method. The following code sample contains the UploadFileToStorage task.

public static async Task<bool> UploadFileToStorage(Stream fileStream, string fileName,
                                                    AzureStorageConfig _storageConfig)
    // Create a URI to the blob
    Uri blobUri = new Uri("https://" +
                          _storageConfig.AccountName +
                          "" +
                          _storageConfig.ImageContainer +
                          "/" + fileName);

    // Create StorageSharedKeyCredentials object by reading
    // the values from the configuration (appsettings.json)
    StorageSharedKeyCredential storageCredentials =
        new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey);

    // Create the blob client.
    BlobClient blobClient = new BlobClient(blobUri, storageCredentials);

    // Upload the file
    await blobClient.UploadAsync(fileStream);

    return await Task.FromResult(true);

The following classes and methods are used in the preceding task:

Class Method
Uri Uri constructor
StorageSharedKeyCredential StorageSharedKeyCredential(String, String) constructor
BlobClient UploadAsync

Verify the image is shown in the storage account

  1. Sign in to the Azure portal. From the left menu, select Storage accounts, then select the name of your storage account.


    Run the following to get the name of the storage account: echo $blobStorageAccount.

  2. On the left menu, in the Data storage section, select Containers.

  3. Select the images blob container.

  4. Verify the image is shown in the container.

    Screenshot of the Container page showing the list of uploaded images.

Test thumbnail viewing

To test thumbnail viewing, you upload an image to the thumbnails container to check whether the app can read the thumbnails container.

  1. Sign in to the Azure portal. From the left menu, select Storage accounts, then select the name of your storage account. Select Containers, then select the thumbnails container. Select Upload to open the Upload blob pane.

  2. Choose a file with the file picker and select Upload.

  3. Navigate back to your app to verify that the image uploaded to the thumbnails container is visible.

    Screenshot of the web app showing the thumbnail image.

  4. In part two of the series, you automate thumbnail image creation so you don't need this image. In the thumbnails container, select the image you uploaded, and select Delete to remove the image.

Next steps