GithubHelp home page GithubHelp logo

cloudinary-simple-upload's Introduction

Fast, easy, package to upload files (images or pdf) to cloudinary.

Getting started

Installation

npm i cloudinary-simple-upload

or

yarn add cloudinary-simple-upload

Basic Usage

First require express and express-fileupload for receiving file requests.
Add your cloudinary cloud_name, api_key and api_secret. Register here cloudinary for your cloudinary credentials.

//  Index.js
const express = require('express');
const fileUpload = require('express-fileupload');
const {config} = require('cloudinary-simple-upload');

const app = express();
app.use(express.json({ extended: false }));
app.use(fileUpload());

config(process.env.CLOUD_NAME, process.env.API_KEY, process.env.API_SECRET);

app.get('/', function (req, res) {
  res.send('Upload')
});

app.listen(3300);

i. Upload a single image

// upload js
const {uploadImage} = require('cloudinary-simple-upload');

app.post('/upload', async (req, res) => {
    if (!req.files) {
        return res.status(400).json({
            msg: 'please upload an image'
        });
    }
   
    try {
        // The name of the input field (i.e. "newFile") is used to retrieve the uploaded file
        let newFile = req.files.newFile;
       
        // "test" is the name of folder the uploaded image will be stored in cloudinary.
        const files = await uploadImage(newFile, "test");
    
       res.status(201).json({
           msg: 'Success',
           files
       });
    } catch (e) {
        console.log(e);
        res.status(500).send('Internal server error...');
    }
    
});

Below is an example of uploaded result

{
        "asset_id": "5cc595d80e78f4088988a8bc490ee9f7",
        "public_id": "test/sav1qegnzxsgxzabow2m",
        "version": 1601302143,
        "version_id": "eda87109f052cf9951844629f538f3a0",
        "signature": "8756cede85a23c62793dc5229ddb151ff6091a21",
        "width": 1200,
        "height": 1177,
        "format": "jpg",
        "resource_type": "image",
        "created_at": "2020-09-28T14:09:03Z",
        "tags": [],
        "bytes": 546268,
        "type": "upload",
        "etag": "4e3ac42ce120bc5503987c1b4687be96",
        "placeholder": false,
        "url": "http://res.cloudinary.com/abdraqeeb/image/upload/v1601302143/test/sav1qegnzxsgxzabow2m.jpg",
        "secure_url": "https://res.cloudinary.com/abdraqeeb/image/upload/v1601302143/test/sav1qegnzxsgxzabow2m.jpg",
        "original_filename": "image1601302140586"
    }

ii. Upload Multiple Images

//pdf.js
const {uploadImages} = require('cloudinary-simple-upload');

app.post('/upload', async (req, res) => {
    if (!req.files || Object.keys(req.files).length < 2) {
        return res.status(400).json({
            msg: 'please upload multiple images'
        });
    }
    
    try {
         // The name of the input field (i.e. "newFile") is used to retrieve the uploaded files
            let newFiles = req.files.newFiles;
            
            // "test" is the name of folder the uploaded image will be stored in cloudinary.
            const files = await uploadImages(newFiles, "test");
        
           res.status(201).json({
               msg: 'Success',
               files
           });
    } catch (e) {
        console.log(e);
        res.status(500).send('Internal server error');
    }
   
});

iii. Upload PDF

// upload js
const {uploadPdf} = require('cloudinary-simple-upload');
...

iv. Upload multiple pdfs

// upload js
const {uploadPdfs} = require('cloudinary-simple-upload');
...

v. Upload single video

// upload js
const {uploadVideo} = require('cloudinary-simple-upload');
...

vi. Upload multiple videos

// upload js
const {uploadVideos} = require('cloudinary-simple-upload');
...

vii. Upload audio

// upload js
const {uploadAudio} = require('cloudinary-simple-upload');
...

viii. Upload multiple audios

// upload js
const {uploadAudios} = require('cloudinary-simple-upload');
...

ix. Upload multiple files of images, videos, audios and pdfs together

// upload js
const {uploadFiles} = require('cloudinary-simple-upload');
...

Options

  • key: return single file uploads as an array of object, value can be any string.
    // returns upload response as an array of object
    const file = await uploadPdf(newFile, "test", "array");

Licensing

MIT

Author

Ajao AbdRaqeeb

cloudinary-simple-upload's People

Contributors

abdraqeeb avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.