how to upload document cloddinary code example

Example 1: how to upload document cloddinary

// util.cloud.ts
import { v2 as cloudinary } from 'cloudinary'

export const cloudStorage = (filename: string): Promise<Record<string, any>> => {
	return new Promise((resolve, reject) => {
		cloudinary.config({
			cloud_name: process.env.CLOUDINARY_NAME,
			api_key: process.env.CLOUDINARY_API_KEY,
			api_secret: process.env.CLOUDINARY_API_SECRET,
			shorten: true,
			secure: true,
			ssl_detected: true
		})

		cloudinary.uploader
			.upload(filename, { resource_type: 'auto' })
			.then((response: UploadApiResponse) => resolve(response))
			.catch((error) => reject(error))
	})
}

// upload.ts
const urls = []
	const image = req.files.image
	const document = req.files.document
	const files = image.concat(document)

	for (let file of files) {
		try {
			const response = await cloudStorage(file.path)
			urls.push(response)
		} catch (err) {
			console.log(err)
		}
	}

Example 2: how to upload document cloddinary

resource_type = auto
please check more about options this -> https://cloudinary.com/documentation/image_upload_api_reference

Tags:

Misc Example