Skip to content

Instantly share code, notes, and snippets.

@tomaspavlic
Last active June 23, 2020 08:30
Show Gist options
  • Save tomaspavlic/03ae97c971686fa7443b9331eccffd0b to your computer and use it in GitHub Desktop.
Save tomaspavlic/03ae97c971686fa7443b9331eccffd0b to your computer and use it in GitHub Desktop.
Upload file or folder to Azure blob storage and shorten uploaded file with bit.ly.
#!/bin/bash
INPUT="$1"
CLEAN=$false
check_dependencies() {
for name in az curl sed tput; do
[[ $(which $name 2>/dev/null) ]] || {
echo -en "$name needs to be installed."
deps=1
}
done
[[ $deps -eq 1 ]] && {
echo -en "\nInstall the above and rerun this script\n"
exit 1
}
}
## Azure configuration
AZURE_STORAGE_CONTAINER_NAME='public'
AZURE_STORAGE_ACCOUNT_NAME='??'
AZURE_STORAGE_ACCOUNT_KEY='??'
## bit.ly configuration
BITLY_USERNAME='??'
BITLY_PASSWORD='??'
BITLY_API_URL='https://api-ssl.bitly.com'
BITLY_API_HOST='api-ssl.bitly.com'
check_dependencies
[[ -z "$1" || $1 == "--help" || $1 == "-h" ]] && echo "usage: abput [directory/file]" && exit 1
if [[ -d $INPUT ]]; then
FILE=$(mktemp)
FILENAME="$(basename "$INPUT").tar.gz"
CLEAN=$true
tar -zcvf $FILE $INPUT 2>/dev/null
else
FILENAME=$(basename "$INPUT")
FILE=$INPUT
fi
## Create container if does not exist
az storage container create \
--name $AZURE_STORAGE_CONTAINER_NAME \
--public-access "blob" \
--account-name $AZURE_STORAGE_ACCOUNT_NAME \
--account-key $AZURE_STORAGE_ACCOUNT_KEY >/dev/null
## Upload the file
az storage blob upload \
--file "$FILE" \
--container-name $AZURE_STORAGE_CONTAINER_NAME \
--name "$FILENAME" \
--account-name $AZURE_STORAGE_ACCOUNT_NAME \
--account-key $AZURE_STORAGE_ACCOUNT_KEY >/dev/null
## Erase progress line
tput cuu1
tput el
## Get URL address of the uploaded file
URL=$(
az storage blob url --container-name $AZURE_STORAGE_CONTAINER_NAME \
--name "$FILENAME" \
--account-name $AZURE_STORAGE_ACCOUNT_NAME \
--account-key $AZURE_STORAGE_ACCOUNT_KEY
)
## Get shorten URL of the uploaded file
PAYLOAD="{ \"domain\": \"bit.ly\", \"long_url\": $URL }"
OAUTH=$(
curl --user "$BITLY_USERNAME:$BITLY_PASSWORD" \
--request POST "$BITLY_API_URL/oauth/access_token" 2>/dev/null
)
curl --request POST \
--data "$PAYLOAD" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $OAUTH" \
--header "Host: $BITLY_API_HOST" "$BITLY_API_URL/v4/shorten" 2>/dev/null | sed -ne 's/.*\(https:\/\/bi[^"]*\).*/\1/p'
## Clean up
[[ $CLEAN ]] && rm $FILE
@tomaspavlic
Copy link
Author

tomaspavlic commented Jun 18, 2020

abput

Upload file or folder to Azure blob storage and shorten uploaded file with bit.ly.

Configuration

Set empty variables in section Azure configuration and bit.ly configuration.

## Azure configuration
AZURE_STORAGE_ACCOUNT_NAME='??'
AZURE_STORAGE_ACCOUNT_KEY='??'

## bit.ly configuration
BITLY_USERNAME='??'
BITLY_PASSWORD='??'

Usage

usage: abput [directory/file]

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