Introduction to Cloudinary
Cloudinary is a cloud-based service that provides an end-to-end image and video management solution. It offers a wide array of APIs that allow developers to upload, manipulate, optimize, and deliver media content in various ways.
Uploading an Image
Upload your images to Cloudinary quickly and efficiently using the following API:
curl -X POST 'https://api.cloudinary.com/v1_1/YOUR_CLOUD_NAME/image/upload' \ -H 'Content-Type: application/json' \ -d '{
"file": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
"upload_preset": "ml_default"
}'
Fetching Uploaded Images
Retrieve the uploaded images using the resource API:
curl -X GET 'https://api.cloudinary.com/v1_1/YOUR_CLOUD_NAME/resources/image' \ -H 'Authorization: Bearer YOUR_ACCESS_TOKEN'
Image Transformations
Transform images dynamically with the following code snippet:
const cloudinary = require('cloudinary').v2;
cloudinary.url('sample.jpg', { width: 500, height: 500, crop: "fill" });
Video Upload
Upload videos similarly to images:
curl -X POST 'https://api.cloudinary.com/v1_1/YOUR_CLOUD_NAME/video/upload' \ -H 'Content-Type: application/json' \ -d '{
"file": "http://res.cloudinary.com/demo/video/upload/dog.mp4",
"upload_preset": "ml_default"
}'
Building an Application with Cloudinary
Let’s create a simple Node.js application that uses the Cloudinary APIs:
const express = require('express'); const cloudinary = require('cloudinary').v2; const fileUpload = require('express-fileupload');
const app = express(); app.use(fileUpload());
cloudinary.config({
cloud_name: 'YOUR_CLOUD_NAME',
api_key: 'YOUR_API_KEY',
api_secret: 'YOUR_API_SECRET'
});
app.post('/upload', (req, res) => {
let imageFile = req.files.file;
cloudinary.uploader.upload(imageFile.tempFilePath, (error, result) => {
if (error) return res.status(500).send(error);
res.json(result);
});
});
app.listen(3000, () => {
console.log('Server started on http://localhost:3000');
});
Run this server and use a client like Postman to test your image upload.
Hash: 4afcef5dd73bf324ddf3a9093d013495cbdb8e1015b0b689f27ab5f617f115bc