Upload to Cloudinary with a Shortcut
Often, I want to take an image that exists locally on my Mac or iPhone and get a URL for it.
- Maybe I want to embed the image on a webpage.
- Maybe I just want to include it in some Markdown.
- Maybe I want to drop a freshly-minted screenshot into a GitHub comment, or bug report.
- Maybe I want to email a photo without worrying about attachment/storage quotas.
I end up doing this, what – five? ten? – times a week? Here’s what I used to do:
- Type “cloudinary.com/console” into the nearest URL bar.
- Enter my username and password.
- Find my iPhone, launch Google Authenticator; memorize & speed-type six digits (😤).
- Click “Media Library”.
- Drag the file into the browser window.
- Find the newly-uploaded asset and click the li’l 🔗 icon.
- Done.
Too many steps! Here’s what I do now:
- Control-click the file.
- “Quick Actions” → “Upload to Cloudinary”
- Done!
How? With this: Upload to Cloudinary Shortcut, on GitHub.
How it works
Cloudinary has an Upload API. Most folks use it via an SDK, which makes things (especially generating authentication signatures) easier. But! I fear/loathe dependencies and Shortcuts (surprisingly?) provides enough functionality that I managed to craft properly-signed HTTP POST requests, using just Shortcuts.
Let’s walk through it.
First things first, let’s set some variables.
First: an image file. If we don‘t have one already (because the Shortcut was run as a Quick Action or from a Share Sheet, on an existing file/photo), we ask for one, from your Photo Library.
Next up: your account’s Cloud Name, API Key, and API Secret. The first time you install the Shortcut, it asks you for these. Your answers get stored in these Text “Actions”, forevermore. No, I'm not going to show you mine.
Now we can go about building our POST request. The first thing we’re going to need is a URL. So we stick your Cloud Name (captured earlier) into a string; simple enough.
Next up: the current Unix (aka Epoch) timestamp. I could have shelled out for this (date '+%s'
) but I don't think that would have worked on iOS (?) and, well, this was more fun.
And now we come to the dreaded authentication signature. Cloudinary’s instructions for generating one sound scary but in this case, at least, all we need is a SHA-1 hash of two things stuck together: ① the timestamp ② our API Secret. Shortcuts can do this! Go Shortcuts, go!
And now, we fire off an HTTP POST request, with the file, API Key, timestamp, and signature all form-encoded into the body of the request. The first time I ran this, I worried Shortcuts wasn’t smart/flexible enough to wrap a binary file up into a POST request containing multipart/form-data
. But you know what? It is! Go! Shortcuts! Go!
Then, we wait. Until Cloudinary spits back some JSON, which Shortcuts for some reason calls a “Dictionary,” but is totally equipped to parse and handle. So, we grab the freshly-minted URL out of the returned “Dictionary”, copy it to the clipboard, and alert the user that our work is done (or show the full JSON response if there was an error).
The End!
Bonus: transformations, too
Once you’ve got a Cloudinary URL, it’s easy enough to start adding transformations to it.
Ninety-five percent of the time I run this thing, I add q_auto
to the URL. Beyond that: need to turn a HEIC from your iPhone into something you can actually share with anyone? Want to turn a 12MP JPEG into an optimized, 1200-pixel-wide AVIF? Heck, need to take a 16:9 something-or-other and smart-crop it to a square around the main subject’s face? All of these abilities, and many more, are just a few keystrokes away.