Stevus.com

Intro to the Cloudinary API

June 17, 2019

I needed to way to upload and serve thousands of images for a website I was designing. I didn’t want to waste storage space on hosting these images on my own servers, so started looking for CDNs that could do the job.

I found Cloudinary and was impressed that they had a free option with a good amount of storage and bandwidth available.

After readying the images, I got the Python library installed, and began to code up a solution.

export CLOUDINARY_URL=cloudinary URL from your account config
export CLOUDINARY_API_KEY=your Cloudinary API key
export CLOUDINARY_API_SECRET=your Cloudinary API secret
export CLOUDINARY_DOMAIN=http://res.cloudinary.com/
export CLOUDINARY_CLOUD_NAME=your Cloudinary cloud name

Then I could go ahead to import the library:

from cloudinary.uploader import upload

Once the environment variables and libraries were imported, all I had to do was call the upload method and I was in business.

response = upload(
    your_external_image_url,
    public_id = "your-folder/%s" % your_cloudinary_file_id,
    use_filename = 1,
    unique_filename = 0
)

The only thing I needed to make sure to store somewhere was the response['version']. This response field is how I’ll reference the uploaded file. Here is the final link I used:

https://res.cloudinary.com/CLOUDINARY_CLOUD_NAME/image/upload/response['version']/your-folder/your_cloudinary_file_id.jpg

All in all, now I have thousands and thousands of images available via CDN.


Steven Wright

Written by Steven Wright who lives and works in Sacramento building useful things.