Skip to content

Instantly share code, notes, and snippets.

@bartekpacia
Created September 26, 2022 20:46
Show Gist options
  • Save bartekpacia/194b801a4a3bd050641b806957b3224e to your computer and use it in GitHub Desktop.
Save bartekpacia/194b801a4a3bd050641b806957b3224e to your computer and use it in GitHub Desktop.
Shell script to easily upload a file to Azure Storage.
#!/usr/bin/env bash
set -euo pipefail
# upload_artifact uploads a file to Azure Storage.
#
# Usage:
#
# AZURE_SAS_TOKEN=SOME_TOKEN ./upload_artifact your_file.txt
artifact="${1:-}"
if [ -z "$artifact" ]; then
echo "File to be uploaded was not passed as the first argument"
exit 1
fi
mime="$(file -b --mime-type "$artifact")"
if [ -z "${AZURE_STORAGE_URL:-}" ]; then
echo "AZURE_STORAGE_URL is not set"
exit 1
fi
if [ -z "${AZURE_SAS_TOKEN:-}" ]; then
echo "AZURE_SAS_TOKEN is not set"
exit 1
fi
filename="$(basename "$artifact")"
url="$AZURE_STORAGE_URL/$filename?$AZURE_SAS_TOKEN"
echo "File $filename will be uploaded to $url"
curl --location -g --request PUT "$url" \
--header "x-ms-blob-type: BlockBlob" \
--header "Content-Type: $mime" \
--data-binary "@$PWD/$artifact"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment