This project demonstrates how to implement signed uploads to Cloudinary using the Upload Widget in a Node.js and Express application.
- Clone the repository:
git clone https://github.com/your-username/cld-signed-uploads.git - Change into the cloned directory:
cd cld-signed-uploads - Install dependencies:
npm install - Create an
.envfile in the root directory and add your Cloudinary credentials:
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
Start the server:
npm start
The application will be available at http://localhost:3000 (or the port specified in your environment variables).
app.js: Main server filepublic/: Static files directoryjs/uploadClientWidget.js: Client-side JavaScript for the Upload Widgetindex.html: Main HTML file
- The server uses Express.js and configures Cloudinary with environment variables.
- It provides an API endpoint
/api/signuploadwidgetthat generates a signature for secure uploads. - The signature is created using the Cloudinary SDK and includes properties for a timestamp, source, and destination folder.
- The server serves static files from the public directory.
- When the DOM is loaded, it fetches the upload signature from the server. This signature is valid for 1 hour from time of creation.
- It configures the Cloudinary Upload Widget with the received signature and other options.
- The widget is initialized and attached to a button with the ID 'upload_widget'.
- On successful upload, it displays the uploaded image or video and the response data.
-
Cloudinary Configuration: The server configures Cloudinary using a Cloudinary Cloud Name, API key, and API secret which are stored in environment variables for secure access.
-
Signature Generation: The
/api/signuploadwidgetendpoint creates a signed request for secure uploads. -
Upload Widget: The client-side code initializes and manages the Cloudinary Upload Widget.
-
Result Handling: After a successful upload, the client-side code displays the uploaded image or video and response data.
- The server provides the necessary API for signature generation which facilitates secure uploads.
- The client-side JavaScript fetches the upload signature from the server.
- The Upload Widget uses this signature to securely upload files to Cloudinary.
- The uploaded image or video is then displayed on the page.
This setup ensures that uploads are secure and authenticated, preventing unauthorized access to your Cloudinary account.
NODE_ENV: Not included in development, but set to 'production' in production environmentsPORT: The port on which the server runs (default: 3000)HOST: The host address (default: 'localhost')CLOUDINARY_CLOUD_NAME: Your Cloudinary Cloud NameCLOUDINARY_API_KEY: Your Cloudinary API keyCLOUDINARY_API_SECRET: Your Cloudinary API secret