How to update a script with azcopy v7 to v10

Here is the previous version of the script to transfer containers between storages.

AzCopy v10 has totally different parameters, in fact, it’s another utility that does the same thing. Instead of --source, --dest parameters, it now requires a SAS token.

#!/bin/bash
CONTAINER_NAME=$1
SOURCE_STORAGE_NAME=$2
SOURCE_STORAGE_KEY=$3
TARGET_STORAGE_NAME=$4
TARGET_STORAGE_KEY=$5

source_exists=$(az storage container exists --name $CONTAINER_NAME --account-name $SOURCE_STORAGE_NAME --account-key $SOURCE_STORAGE_KEY --output tsv)
if [ $source_exists != "True" ]; then
    echo "Source container does not exist." 1>&2
    exit 1
fi;

access_level=$(az storage container show-permission -n $CONTAINER_NAME --account-name $SOURCE_STORAGE_NAME --account-key $SOURCE_STORAGE_KEY --output tsv)
target_exists=$(az storage container exists --name $CONTAINER_NAME --account-name $TARGET_STORAGE_NAME --account-key $TARGET_STORAGE_KEY --output tsv)
if [ $target_exists != "True" ]; then
    az storage container create --name $CONTAINER_NAME --public-access $access_level --account-name $TARGET_STORAGE_NAME --account-key $TARGET_STORAGE_KEY
fi;

expiry=$(python -c "from datetime import datetime, timedelta; print((datetime.utcnow() + timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M:%SZ'))")
source_sas=$(az storage container generate-sas --name $CONTAINER_NAME --expiry $expiry --permissions lr --account-name $SOURCE_STORAGE_NAME --account-key $SOURCE_STORAGE_KEY -o tsv)
target_sas=$(az storage container generate-sas --name $CONTAINER_NAME --expiry $expiry --permissions aclrw --account-name $TARGET_STORAGE_NAME --account-key $TARGET_STORAGE_KEY -o tsv)
azcopy copy https://$SOURCE_STORAGE_NAME.blob.core.windows.net/$CONTAINER_NAME?$source_sas https://$TARGET_STORAGE_NAME.blob.core.windows.net/$CONTAINER_NAME?$target_sas --recursive 

To run this script in Docker container:

FROM mcr.microsoft.com/azure-cli:2.7.0

RUN wget https://aka.ms/downloadazcopy-v10-linux -O /tmp/azcopy.tgz \
    && export BIN_LOCATION=$(tar -tzf /tmp/azcopy.tgz | grep "/azcopy") \
    && tar -xzf /tmp/azcopy.tgz $BIN_LOCATION --strip-components=1 -C /usr/local/bin
docker build -t azure-cli .
docker run --rm -it -v `pwd`/script.sh:/script.sh azure-cli bash
comments powered by Disqus