Created
April 22, 2022 03:14
-
-
Save Melbourneandrew/b038587f56102fbf41d745485c121ea7 to your computer and use it in GitHub Desktop.
Upload to Cloudflare images with Axios NodeJS
This file contains hidden or 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
const FormData = require('form-data'); | |
const { default: axios } = require("axios"); | |
const deliveryURL = "<CF Image Delivery URL>" | |
const uploadURL = "<CF Image Upload URL>" | |
const cf_api_token = "<api_token>" | |
const form = new FormData(); | |
form.append("file", <image file object>, "<image name for Cloudflare>") | |
const cfResponse = await axios.post( | |
uploadURL, | |
form, | |
{ | |
headers:{ | |
...form.getHeaders(), | |
Authorization: `Bearer ${cf_api_token}` | |
} | |
} | |
) | |
const imageURL = uploadURL + `/${cfResponse.data.result.id}` + '/public' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment