Node.js image and video upload
Last updated: Sep-02-2024
Cloudinary provides an API for uploading images, videos, and any other kind of file to the cloud. Files uploaded to Cloudinary are stored safely in the cloud with secure backups and revision history. Cloudinary's APIs allow secure uploading from your servers, directly from your visitors' browsers or mobile applications, or fetched via remote public URLs.
Cloudinary's Node.js SDK wraps Cloudinary's upload API and simplifies the integration. Node.js methods are available for easily performing Node.js image and video uploads to the cloud and Node.js helper methods are available for uploading directly from a browser to Cloudinary.
This page covers common usage patterns for Node.js image and video upload with Cloudinary.
For details on all available upload functionality, see the Upload guide, and the upload method of the Upload API Reference.
Server-side upload
You can upload images, videos, or any other raw file to Cloudinary from your Node.js code. Uploading is done over HTTPS using a secure protocol based on your api_key
and api_secret
parameters.
Programmatic upload video tutorial
Watch this demo on how to quickly upload images, videos and other media files to Cloudinary for immediate deliverability using Cloudinary's Upload API in your development environment.
Tutorial contents
Node.js image upload
The following Node.js method uploads an image to the cloud:
For example, uploading a local image file named 'my_image.jpg':
The file to upload can be specified as a local path, a remote HTTP or HTTPS URL, a whitelisted storage bucket (S3 or Google Storage) URL, a base64 data URI, or an FTP URL.
upload_stream
method for uploading data streams, see below.For details and code examples of uploading using each of these data source types, see Required upload parameters.
For details on all available upload functionality, see the Upload guide, and the upload method of the Upload API Reference.
process.on('unhandledRejection')
, you can disable the Cloudinary internal promises by also including the disable_promise
parameter set to true
.Node.js upload_stream
The dedicated upload_stream
SDK method is used to upload a data stream. The method is asynchronous so we recommend wrapping the function in a promise.
For example:
Using await
:
Node.js video upload
You upload videos in the same way as images. However, with videos, you must specify the resource_type
as 'video' within the upload
method. In addition, the upload
method supports uploading files only up to 100 MB. To upload larger videos, use the upload_large method, which uploads large files to the cloud in chunks.
The upload_large
method has the identical signature and options as the upload
method, with the addition of an optional chunk_size
parameter (default 20 MB).
The following example uploads dog.mp4
to Cloudinary and stores it with the public ID dog_closeup
. It also performs two eager transformations that resize the video to a square and a small rectangle.
upload_large_stream
method to upload a stream, rather than a file.Upload response
By default, uploading is performed synchronously. Once finished, the uploaded image or video is immediately available for transformation and delivery. An upload call returns a Hash with content similar to the following:
The response includes HTTP and HTTPS URLs for accessing the uploaded media asset as well as additional information regarding the uploaded asset: The public ID, resource type, width and height, file format, file size in bytes, a signature for verifying the response and more.
Direct uploading from the browser
The upload sample mentioned above allows your server-side Node.js code to upload media assets to Cloudinary. In this flow, if you have a web form that allows your users to upload images or videos, the media file's data is first sent to your server and only then uploaded to Cloudinary.
A more efficient and powerful option is to allow your users to upload images and videos in your client-side code directly from the browser to Cloudinary instead of going through your servers. This method allows for faster uploading and a better user experience. It also reduces load from your servers and reduces the complexity of your Node.js applications.
You can upload directly from the browser using signed or unsigned calls to the upload endpoint, as shown in the Upload multiple files using a form examples.
For signed uploads from your client-side code, a secure signature must be generated in your server-side Node.js code. You can use the api_sign_request
method to generate SHA signatures: