Skip to content

Instantly share code, notes, and snippets.

@ghidalgo3
Last active December 3, 2024 16:39
Show Gist options
  • Save ghidalgo3/ef4c691bb700a0acd5c60ae61e2ef35f to your computer and use it in GitHub Desktop.
Save ghidalgo3/ef4c691bb700a0acd5c60ae61e2ef35f 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" | 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
@ghidalgo3
Copy link
Author

ghidalgo3 commented Nov 25, 2024

To receive tokens with longer durations, append a duration query parameter to the request with a value in minutes. The maximum value is 7 days (10080 minutes), though you are free to request shorter lived tokens if you want. Example:

curl -H "Ocp-Apim-Subscription-Key: $k" https://planetarycomputer.microsoft.com/api/sas/v1/token/ai4edataeuwest/gbif\?write\=true\&duration\=10080

This should work for all of the account/container pairs NOAA publishes data to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment