Last active
November 24, 2020 12:19
-
-
Save blackmann/457e1f9f0ec31f3450b6d71ce15ddcc0 to your computer and use it in GitHub Desktop.
Cloudinary upload utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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