Skip to content

Instantly share code, notes, and snippets.

@blackmann
Last active November 24, 2020 12:19
Show Gist options
  • Save blackmann/457e1f9f0ec31f3450b6d71ce15ddcc0 to your computer and use it in GitHub Desktop.
Save blackmann/457e1f9f0ec31f3450b6d71ce15ddcc0 to your computer and use it in GitHub Desktop.
Cloudinary upload utility
import axios from 'axios'
const CLOUNDINARY_URL = 'https://api.cloudinary.com/v1_1/<CLOUD_NAME>/image/upload/'
const UPLOAD_PRESET = 'XXXXXXX'
export default async (file) => {
const formData = new FormData()
formData.append('file', file)
formData.append('upload_preset', UPLOAD_PRESET)
try {
const res = await axios.post(CLOUNDINARY_URL, formData)
const data = res.data
return {
url: data.secure_url,
thumbnail: data.eager[0].secure_url,
type: 'image'
}
} catch (err) {
throw Error('Failed to upload image')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment