Exercise - Move Azure Storage blobs using the Azure CLI
Use the Azure CLI to move blobs from one storage account to another. If you have many blobs, you can perform the move as a batch operation. Additionally, you can apply the --if-unmodified-since
parameter to only copy blobs that aren't recently changed.
In the example scenario, you want to move blobs that contain the details for product specifications, and that are more than six months old, to an archive store in a separate Blob Storage account. You can use a Cool storage account for this purpose; it's more cost effective for holding rarely accessed files than keeping them in Hot storage.
In this exercise, you use the Azure CLI to migrate blobs that are unchanged in the last six months to a separate storage account.
Create and add data to hot storage
The free sandbox allows you to create resources in a subset of the Azure global regions. Select a region from this list when you create resources:
- westus2
- southcentralus
- centralus
- eastus
- westeurope
- southeastasia
- japaneast
- brazilsouth
- australiasoutheast
- centralindia
Create environment variables for your storage account name and region. Replace
<location>
with a region from the previous list.HOT_STORAGE_NAME=hotstorage$RANDOM LOCATION=<location>
Next, run the following command to create a storage account to hold blobs.
az storage account create \ --location $LOCATION \ --name $HOT_STORAGE_NAME \ --resource-group "<rgn>[Sandbox resource group]</rgn>" \ --sku Standard_RAGRS \ --kind BlobStorage \ --access-tier Hot
Obtain the keys for your storage account.
az storage account keys list \ --account-name $HOT_STORAGE_NAME \ --resource-group "<rgn>[Sandbox resource group]</rgn>" \ --output table
Create an environment variable for your account key. Use the value of the first key retrieved by the previous command.
HOT_KEY="<source account key>"
Create a container named specifications in your storage account.
az storage container create \ --name specifications \ --account-name $HOT_STORAGE_NAME \ --account-key $HOT_KEY
Run the following command to download the sample files for this exercise.
git clone https://github.com/MicrosoftDocs/mslearn-copy-move-blobs-from-containers-or-storage-accounts sample
Upload the files to your storage account and save each one as a blob. This command uploads several specification files.
az storage blob upload-batch \ --destination specifications \ --pattern "*.md" \ --source ~/sample/specifications \ --account-name $HOT_STORAGE_NAME \ --account-key $HOT_KEY
Verify that the blobs were created.
az storage blob list \ --container-name specifications \ --output table \ --account-name $HOT_STORAGE_NAME \ --account-key $HOT_KEY
Create a cool storage account
Now we create a second storage account and move data between accounts.
Create an environment variable for your cool storage account name.
COOL_STORAGE_NAME=coolstorage$RANDOM
Create a storage account for holding the archived blobs. Use the Cool access tier. As before, specify an appropriate region, and choose a unique name for your storage account.
az storage account create \ --location $LOCATION \ --name $COOL_STORAGE_NAME \ --resource-group "<rgn>[Sandbox resource group]</rgn>" \ --sku Standard_RAGRS \ --kind BlobStorage \ --access-tier Cool
Obtain the keys for your storage account.
az storage account keys list \ --account-name $COOL_STORAGE_NAME \ --resource-group "<rgn>[Sandbox resource group]</rgn>" \ --output table
Create an environment variable for the cool account key; use the value of the first key retrieved by the previous command:
COOL_KEY="<source account key>"
Create a container named archived-specifications in your storage account. Provide the name of the destination storage account and key as parameters to this command. Otherwise, the container is created in the source storage account.
az storage container create \ --name archived-specifications \ --account-name $COOL_STORAGE_NAME \ --account-key $COOL_KEY
Verify that the destination container is empty. The following command shouldn't display any output.
az storage blob list \ --container-name archived-specifications \ --account-name $COOL_STORAGE_NAME \ --account-key $COOL_KEY \ --output table
Copy blobs to cool storage
Batch-copy the blobs from the specifications container in the source storage account, to the archived-specifications container in the destination storage account. Use the
--dryrun
flag to see which blobs would be copied, without actually copying them.az storage blob copy start-batch \ --destination-container archived-specifications \ --account-name $COOL_STORAGE_NAME \ --account-key $COOL_KEY \ --source-account-name "$HOT_STORAGE_NAME" \ --source-account-key $HOT_KEY \ --source-container specifications \ --dryrun
Review the list of blobs to copy.
Repeat the blob copy command, this time without the
--dryrun
parameter used in the previous step. This time, the blobs are copied.az storage blob copy start-batch \ --destination-container archived-specifications \ --account-name $COOL_STORAGE_NAME \ --account-key $COOL_KEY \ --source-account-name "$HOT_STORAGE_NAME" \ --source-account-key $HOT_KEY \ --source-container specifications
Run the following command and verify that the blobs were copied to the destination container.
az storage blob list \ --container-name archived-specifications \ --account-name $COOL_STORAGE_NAME \ --account-key $COOL_KEY \ --output table