Curl Shell Script To Purge Cache From Bitgravity Content Delivery Network

A content delivery network or content distribution network (CDN) is a system of computers containing copies of data, placed at various points in a network so as to maximize bandwidth for access to the data from clients throughout the network. Please note that the following script will only work with bitgravity CDN. You need to get API access keys using bitgravity portal.

#!/bin/bash
# A simple shell script to purge Bitgravity cdn cache using API and curl 
# command line. Tested on RHEL 6.x, Debian 6.x, and Ubuntu Linux
# ----------------------------------------------------------------------------
# Written by Vivek Gite <http://www.cyberciti.biz/>
# (c) 2012 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 22/May/2012
# ----------------------------------------------------------------------------
 
######*** SET ME FIRST ***#####
# api keys #
akid="you@example.com_or_YOUR-API-USERNAME"
akpass="YOUR-API-PASSWORD"
 
 
# 1 disable it, 0 enable it - show debug info?
_debug=1
 
# 1 disable it, 0 enable it - only show success / fail message?
_quite=0
 
# either curl or wget
_purge="/usr/bin/curl -I"
 
 
## CDN domain ##
origin="$1"
 
## file ##
file="$2"
myurl="${origin}${file}"
 
 
[ $# -eq 0 ] && { echo "Usage $0: cdn.example.net /path/to/file.js"; exit 1 ; }
 
## Calculate md5 ##
md5="${akpass}/service/?api=proxycache&method=purge&object=${myurl}&accessKeyId=${akid}"
hash="$(echo -n "$md5" | md5sum )"
 
## Build final url for $_curl ##
url="http://wsa-api.bitgravity.com/service/?api=proxycache&method=purge&object=${myurl}&accessKeyId=${akid}&h=${hash}"
 
 
## Show debug info ? ##
[ $_debug -eq 0 ] && { 
### debug ###
echo
echo "************** [ DEBUG  ] ***********************"
echo "MD5 url: $md5"
echo "MD5 hash: $hash"
echo "$url"
echo "**************************************************"
echo 
}
 
## Purge it ##
[ $_quite -eq 0 ]  &&  $_purge  -I  "$url" || { $_purge  -I  "$url" &>/dev/null && echo "Sucess with HTTP/200" || echo "Failed."; }

How do I access this script?

Simply run it as follows to purge a cdn url called http://files.cyberciti.biz/static/css/layout.css:
$ ./script files.cyberciti.biz /static/css/layout.css
Sample outputs:

HTTP/1.1 200 OK
Date: Tue, 22 May 2012 10:07:25 GMT
Server: Apache
Set-Cookie: PHPSESSID=xxxxxxxxxxxxxxxxx; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: user_id=you%40example.com; path=/; domain=.wsa-api.bitgravity.com
Set-Cookie: ahash=yyyyyyyyyyyyyyyyyyyyy; path=/; domain=.wsa-api.bitgravity.com
X-Served-By: util10.sjc1.bitgravity.com
Connection: close
Content-Type: text/plain

How do I purge lage number of urls?

Create a text file called urls.txt with urls as follows one per line:

files.cyberciti.biz /path/to/js/1.js
files.cyberciti.biz /path/to/js/2.js
files.cyberciti.biz /path/to/js/3.js
files.cyberciti.biz /static/css/foo.jpg
cdn.cyberciti.biz /dyn/bar.html
video.cyberciti.biz /path/to/video/1
video.cyberciti.biz /path/to/video/1.flv

Run the script as follows:

#!/bin/bash
# bulk_purge.sh
# Written by Vivek Gite <http://www.cyberciti.biz/>
# (c) 2012 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------
_purge="/path/to/purge_script.sh"
_urls="/path/to/urls.txt"
while IFS= read -r domain file
do 
    $_purge "$domain" "$file"
done < "${_urls}"

Run it as follows:
$ ./bulk_purge.sh

Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Vasi Ur Rahman Sep 9, 2020 @ 5:18

    Need the same using JAVA API’s

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.