Table of Contents
The useUpload
hook combined with a serverless function allows you to upload your files with ease to your Cloudinary account.
import { useUpload } from 'use-cloudinary'function SignedUpload() {const {upload, data, isLoading, isError, error } = useUpload({ endpoint: "/your/endpoint" });if (isLoading) return <p>Loading...</p>;if (isError) return <p>{error.message}</p>;return (<div><input type="file" onChange={() => {// ...stuff to make sure your media is ready to uploadupload({file,uploadOptions});}} />{data && <img src={data.url} />}</div>)}function UnsignedUpload() {const {upload, data, status, error } = useUpload({ endpoint: "/your/endpoint" });if (isLoading) return <p>Loading...</p>;if (isError) return <p>{error.message}</p>;return (<div><input type="file" onChange={() => {// ...stuff to make sure your media is ready to uploadupload({file,uploadOptions,// the only difference for unsigned uploads are these two keys. these will inform a backend function on which function from the SDK to useunsigned: "true",uploadPreset: "upload-preset-1"});}} />{data && <img src={data.url} />}</div>)}
An example serverless function you'd set up looks like this --
const cloudinary = require('cloudinary').v2;cloudinary.config({cloud_name: process.env.CLOUD_NAME,api_key: process.env.API_KEY,api_secret: process.env.API_SECRET})// When doing a signed upload, you'll use a function like thisexports.handler = (event) => {const { file } = JSON.parse(event.body);const res = await cloudinary.uploader.upload(file, { ...JSON.parse(event.body);})return {statusCode: 200,body: JSON.stringify(res)}}// When doing unsigned, you'll want to use a function like this oneexports.handler = event => {const { file } = JSON.parse(event.body);// The difference of using an unsigned uploads comes down to the options it accepts for the uploadconst res = await cloudinary.uploader.unsigned_upload(file, {...JSON.parse(event.body)});return {statusCode: 200,body: JSON.stringify(res)}}
Args
Key | Type | Required? | Description |
---|---|---|---|
endpoint | String | Yes | An endpoint that will accept your file and options and upload to Cloudinary |
Return values
Key | Type | Description |
---|---|---|
upload | Func | An async function that that will pass our file and any transform options to our serverless function |
data | undefined || String | When upload is successful, data will return the value of the request. More about this here |
isLoading | Boolean | isLoading state returns a boolean if the query is in a "hard" loading state. This means no cached data and the query is currently fetching.here |
isErrro | Boolean | isError returns a boolean (true) if the query attempt resulted in an error (otherwise false). More about React Query return vairables here |
isSuccess | Boolean | isSuccess returns a boolean (true) if the query attempt resulted in an error (otherwise false).More about React Query return vairables here |
isIdle | Boolean | isIdle returns a boolean (true) if the query is initialized with enabled: false and no initial data is available (otherwise false). More about React Query return vairables here |
error | Object | When upload is successful, data will return an object with details about your successfully uploaded media. More about that structure can be found here |
upload()
The upload
function is simply a setState
call to pass your file and transformation options to our endpoint.
Key | Type | Required? | Description |
---|---|---|---|
file | String | Yes | The file to upload. It can be:
|
uploadOptions | Object | The `uploadOptions` object is how you'll pass configuration to your serverless endpoint. For all of the options look here |
Check this out for a full list of optional parameters.
Edit this page on GitHub