Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shaunakv1/62c9669dcdacde1dfee01dc70d54e149 to your computer and use it in GitHub Desktop.
Save shaunakv1/62c9669dcdacde1dfee01dc70d54e149 to your computer and use it in GitHub Desktop.
# Print all commands
set -x
# Stop on error
set -e
# This key was shared over email, treat it as a secret.
SAS_SUBSCRIPTION_KEY=""
# Example (account, container) pair.
account="nclimgrideastus"
container="nclimgrid"
# Assume this file exists
file_name="test.txt"
echo "Hello" > $file_name
# You will need `jq` or simply a way to parse JSON and extract the token
sas_token=$(curl -s -H "Ocp-Apim-Subscription-Key: ${SAS_SUBSCRIPTION_KEY}" "https://planetarycomputer.microsoft.com/api/sas/v1/token/$account/$container?write=True&duration=10080" | jq -r .token)
# When you pass write=True to the SAS API, the issued token will have write permissions
# at the _container_ level. If you need to write to multiple locations, simply request
# multiple tokens and use the correct token for the correct container.
# If you don't pass write=True, you will get a read-only token.
# You can inspect the `sp` parameter of the token to see its permissions (e.g. read, write, delete [by their first letter])
# Uploading files
# OPTION 1: Using azcopy (PREFERRED)
# https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10?tabs=dnf#install-azcopy-on-linux-by-using-a-package-manager
azcopy copy "https://$account.blob.core.windows.net/$container/$file_name?$sas_token" $file_name
# azcopy copy has **many** variations and options, you can run `azcopy copy --help` to view a full list of options.
# azcopy can traverse directories and upload files in parallel, which can be very useful for large datasets.
# OPTION 2: Using the Azure CLI
az storage blob upload -c $container -f $file_name -n $file_name --sas-token "$sas_token" --account-name $account --overwrite
# You can run `az storage blob upload --help` to view a full list of arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment