Skip to content

Uploading Images in Node.js With the Cloudinary Node SDK

Managing your image and video workflow is crucial, and a significant aspect of it involves ensuring your media is properly organized in your Cloudinary media library. In this Dev Hint, we’ll guide you on using the Node SDK for this purpose. We’ll also cover the process of uploading single or multiple images using the Node SDK uploader. After going through this, you’ll be well-equipped to seamlessly transfer your images and videos to the cloud.

To get started with Cloudinary in your Node.js application, follow these steps:

First, you need to install the Cloudinary Node module using either npm or yarn. This module will enable your Node.js application to interact with Cloudinary services.

npm install cloudinary
# or
yarn add cloudinaryCode language: PHP (php)

Once you’ve installed the module, import it into your Node.js app. This allows your application to use Cloudinary’s functionalities.

Then, connect your Node.js application to your Cloudinary account. You’ll need to configure it with your Cloud Name, API Key, and API Secret. You can find these credentials in your Cloudinary dashboard.

const cloudinary = require('cloudinary').v2;
# or
import { v2 as cloudinary } from 'cloudinary' 

cloudinary.config({
  cloud_name: 'your_cloud_name',
  api_key: 'your_api_key',
  api_secret: 'your_api_secret',
});

const image = './path/to/image.jpg'; // This can also be a remote URL or a base64 DataURICode language: PHP (php)

Finally, specify the source of the image you wish to upload, as shown above.

Note:

This source can be either a local image file path, a remote URL, or a base64 DataURI, offering flexibility in how you provide the image for upload.

With Cloudinary configured in our application and the source of our image defined, we’re all set to upload the image to our media library. The code below allows us to achieve this seamlessly:

const result = await cloudinary.uploader.upload(image);Code language: JavaScript (javascript)

The result will include information about the uploaded image, such as its URL, secure URL, public ID, and various metadata, depending on the Cloudinary configuration and the specific options used for the upload.

For uploading multiple images, you can employ a loop, typically a “for” loop, to process each image one by one. Here’s an example:

for (const image of images) {
const result = await cloudinary.uploader.upload(image);
console.log(<code>Successfully uploaded ${image}</code>);
console.log(<code>> Result: ${result.secure_url}</code>);
}Code language: JavaScript (javascript)

This looping process will use the uploader to upload your image. It allows you to efficiently manage and upload multiple images one at a time, ensuring each image is successfully uploaded before moving on to the next.

Looking at the documentation, the uploader has the capability of accepting formats aside from the local path. You can pass a base 64 data URI or even copy the image URL directly into the uploader. This functionality provides a quick and easy way to upload images that exist on other servers to your Cloudinary account.

In a nutshell, to upload images to your Cloudinary account using the Node SDK, you just need to get the path or URL of your images and pass it to the uploader. It offers an incredibly easy way to move our local or online images into the Cloudinary cloud. Indeed, uploading images and videos to our Cloudinary account has never been simpler!

If you found this article helpful and want to discuss it in more detail, head over to Cloudinary Community forum and its associated Discord.

Back to top

Featured Post