Curl Shell Script To Purge Cache From Stackpath CDN
#!/usr/bin/env bash
# Usage:
# A simple shell script to purge Stackpath cdn cache using API and curl
# command line. Tested on Ubuntu/Debian/RHEL/Fedora Linux and FreeBSD/macOS Unix
# Syntax:
# /path/to/stackpath.purge.sh https://your-domain/url
# ----------------------------------------------------------------------------
# Written by Vivek Gite <http://www.cyberciti.biz/>
# (c) 2020 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 22/May/2012
# ----------------------------------------------------------------------------
set -e
################
# Set me first #
################
stack_id="YOUR-STACK-ID-HERE"
# APK keys
client_id='YOUR-API-CLIENT-ID-HERE'
client_pwd='YOUR-API-AUTH-PASSWORD-HERE'
######################
## No editing below ##
######################
# All ulrs
urls="$@"
# Make sure we get at least url else die with usage
[ "$urls" == "" ] && { echo "Usage: $0 url1 url2 ..."; exit 1; }
# Get auth stuff; aka bearer
b="$(curl -s --request POST \
--url https://gateway.stackpath.com/identity/v1/oauth2/token \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data "{
\"client_id\": \"${client_id}\",
\"client_secret\": \"${client_pwd}\",
\"grant_type\": \"client_credentials\"
}" | jq '.access_token')"
# Clean up bearer for the curl
bearer="${b//\"/}"
# Let us start purging urls one-by-one
for u in $urls
do
echo -n "Purgin url $u:"
purge_id=$(curl -s --request POST \
--url https://gateway.stackpath.com/cdn/v1/stacks/$stack_id/purge \
--header 'accept: application/json' \
--header "authorization: Bearer $bearer" \
--header 'content-type: application/json' \
--data "{\"items\":[
{
\"url\":\"${u}\",
\"purgeAllDynamic\":true
}
]
}" | jq '.id')
# Get url purge status
purge_id="${purge_id//\"/}"
curl -s -i --request GET \
--url https://gateway.stackpath.com/cdn/v1/stacks/$stack_id/purge/$purge_id \
--header 'accept: application/json' \
--header "authorization: Bearer $bearer" | egrep -o 'HTTP/.*|{.*}'
echo ""
done
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line