Skip to content

Instantly share code, notes, and snippets.

@ralf-br
Last active May 6, 2021 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralf-br/e287894b8e4e3807760e3ca998b76635 to your computer and use it in GitHub Desktop.
Save ralf-br/e287894b8e4e3807760e3ca998b76635 to your computer and use it in GitHub Desktop.
Azure File Storage: Move a large quantity of files from one folder to another (on the same fileshare)
#!/bin/zsh
# Works in zsh-shell on Mac.
# Requires azcopy (https://formulae.brew.sh/formula/azcopy) and az (https://formulae.brew.sh/formula/azure-cli#default)
# As there is no move we copy and rm the files. (The copy is blocking so the remove will not be executed on failure)
export ACCOUNTNAME=xxxx
export ACCOUNTKEY='xxxxxxayyyyyyyyyyyyyyy=='
export FILESHARE=fileshare
export PATHFROM=path/from/*
export PATHTO=path
export INCLUDEPATTERN=* #can be also sth like *_202010*;*_202011* to filter files (by name) for October/November
#https://docs.microsoft.com/en-us/cli/azure/storage/share?view=azure-cli-latest#az_storage_share_generate_sas
SAS=$(az storage share generate-sas \
--name $FILESHARE \
--account-name $ACCOUNTNAME \
--account-key $ACCOUNTKEY \
--permissions rwdl \
--expiry 2021-12-31T23:59:00Z \
--https-only \
| sed -E 's/^\"(.*)\"$/?\1/') #remove "" and add ? at the beginning otherwise we cannot directly use the SAS token
#https://docs.microsoft.com/en-us/azure/storage/common/storage-ref-azcopy
azcopy copy "https://$ACCOUNTNAME.file.core.windows.net/$FILESHARE/$PATHFROM$SAS" \
"https://$ACCOUNTNAME.file.core.windows.net/$FILESHARE/$PATHTO$SAS" \
--include-pattern $INCLUDEPATTERN \
&& azcopy rm "https://$ACCOUNTNAME.file.core.windows.net/$FILESHARE/$PATHFROM$SAS" \
--include-pattern $INCLUDEPATTERN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment