Skip to content

Instantly share code, notes, and snippets.

@Avi-E-Koenig
Created March 14, 2023 14:00
Show Gist options
  • Save Avi-E-Koenig/4299c565396c5e35c6bf1ee4cf31c7e1 to your computer and use it in GitHub Desktop.
Save Avi-E-Koenig/4299c565396c5e35c6bf1ee4cf31c7e1 to your computer and use it in GitHub Desktop.
basic cloudinary crud
import cloudinary from 'cloudinary';
import fs from 'fs';
import { CLOUDINARY_API_KEY, CLOUDINARY_API_SECRET, CLOUDINARY_CLOUD_NAME } from '@/config';
cloudinary.v2.config({
cloud_name: CLOUDINARY_CLOUD_NAME,
api_key: CLOUDINARY_API_KEY,
api_secret: CLOUDINARY_API_SECRET,
});
type ResultImageData = {
public_id: string;
secure_url: string;
};
const uploadImage = async (filePath: string): Promise<ResultImageData> => {
const result = await cloudinary.v2.uploader.upload(filePath);
fs.unlinkSync(filePath);
return result;
};
const updateImage = async (filePath: string, public_id: string): Promise<ResultImageData> => {
const result = await cloudinary.v2.uploader.upload(filePath, { public_id });
fs.unlinkSync(filePath);
return result;
};
const deleteImage = async (public_id: string) => {
await cloudinary.v2.uploader.destroy(public_id);
return true;
};
export default {
uploadImage,
updateImage,
deleteImage,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment