Upload Multiple Files to an Azure Storage Account Using Azure CLI

This Azure CLI blog post will show how to upload multiple files from a folder to an Azure storage account using Azure CLI.

Uploading multiple files to an Azure storage account using Azure CLI helps automate the process of programmatically uploading large amounts of files.

Azure CLI, like Azure PowerShell, allows us to automate and batch tasks using native Azure tools.

Azure CLI


In the example below, we upload all the files stored in a local directory to an Azure storage account. To run the command successfully, you will need to do the following:

  • Storage account name
  • Storage account key
  • Path to a local directory where the files are stored
  • Storage account container name

Once you have all the above details, log in to Azure using the az login and run the command

az storage blob upload-batch --account-name <storage-account-name> --account-key <storage-account-key> --source <local-directory-path> --destination <container-name>

Complete Script

Please find a script below that performs the following tasks:

  • Set the source folder path
  • Creates a resource group with the specified name and location
  • Creates a storage account with the specified name, resource group, location, and SKU
  • Creates a storage container with the specified name under the specified storage account
  • Uploads all files and directories from the specified source folder to the specified storage container

# Set the source folder path (do the same for all other variables in the script)
$SOURCE_FOLDER="data"


# Creates a resource group with the specified name and location
az group create --name $RESOURCE_GROUP --location $LOCATION --output table

# Creates a storage account with the specified name, resource group, location, and SKU
az storage account create --name $STORAGE_ACCOUNT_NAME --resource-group $RESOURCE_GROUP --location $LOCATION --sku Standard_LRS --output table

# Creates a storage container with the specified name under the specified storage account
az storage container create --name $STORAGE_CONTAINER_NAME --account-name $STORAGE_ACCOUNT_NAME --output table

# Uploads all files and directories from the specified source folder to the specified storage container
az storage blob upload-batch --destination $STORAGE_CONTAINER_NAME --source $SOURCE_FOLDER --account-name $STORAGE_ACCOUNT_NAME --output table


Posted

in

, ,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.