Bash Scripts for Cloudinary

Cloudinary is an amazing image hosting service that offers both a highly functional free tier in addition to a paid enterprise grade image hosting and transformation service.

In this tutorial I demonstrate a few simple bash shell scripts to interface with Cloudinary.

Bash Script Example 1 - Upload most recent OS X Screenshot

This script will take the most recent Mac OS X Screenshot file and upload it to Cloudinary and provide the user with a Cloudinary hosted URL to the image. This is incredibly useful for sharing quick screenshots or partial screenshot with remote users.

The script doesn’t require any command line parameters. It simply finds the most recent Screenshot file (if any) and returns a URL. In OS X you can create a screenshot using the key sequence SHIFT+COMMAND+4 The screenshot file naming convention is the OS X default:
Screen\ Shot\ date/time.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash

export CLOUDINARY_NAME=your_username_id
export CLOUDINARY_PRESET=screenshots
#export CLOUDINARY_PRESET=preset1 # This preset uploads to /icons
today=$(date +%Y-%m-%d)

# if we pass a value into the script, presume its an annotated skitch files and use underscores instead of space/hyphen combo
if [ $# -eq 0 ]; then
filename=$(ls -ltr ~/Desktop/Screen\ Shot\ $today*.png | tail -n1 | tr -s " " | cut -f9- -d" ")
else
filename=$(ls -ltr ~/Desktop/Screen_Shot_$today*.png | tail -n1 | tr -s " " | cut -f9- -d" ")
fi

NEWURL=$(curl -ks -F "upload_preset=$CLOUDINARY_PRESET" -F "file=@$filename" https://api.cloudinary.com/v1_1/$CLOUDINARY_NAME/image/upload | jq -r .secure_url)
echo -e "\n\n$NEWURL\n\n";

# Example:
# curl -ks -F upload_preset=preset1 -F file=@/Users/cbergeron/Desktop/Screen\ Shot\ 2018-09-15\ at\ 12.43.34\ PM.png https://api.cloudinary.com/v1_1/your_username_id/image/uploadcbergeron@2013

Bash Example 2 - Upload image from command line

1
2
3
4
5
6
7
#!/bin/bash

export CLOUDINARY_NAME=cyberge
export CLOUDINARY_PRESET=preset1
NEWURL=$(curl -ks -F "upload_preset=$CLOUDINARY_PRESET" -F "file=@$1" https://api.cloudinary.com/v1_1/$CLOUDINARY_NAME/image/upload | jq -r .url)
echo -e "\n\n$NEWURL\n\n";

Technology used:

Bash ShellCloudinaryCurl
Author

Chris Bergeron

Posted on

03-13-2020

Updated on

07-01-2022

Licensed under

Comments