It turns out I just needed to amend that last line from that blog post function to busboy.write(Buffer.from(event.body, 'base64'))
All working! For future searchers, here is my working netlify function to cloudflare image script
import Busboy from 'busboy'
import axios from 'axios'
import FormData from 'form-data'
function parseMultipartForm(event) {
return new Promise((resolve) => {
const fields = {}
const busboy = Busboy({
headers: event.headers,
})
busboy.on('file', (fieldname, filestream, filename, _, mimeType) => {
filestream.on('data', (data) => {
fields[fieldname] = {
content: data,
filename,
type: mimeType,
}
})
})
busboy.on('field', (fieldName, value) => {
fields[fieldName] = value
})
busboy.on('finish', () => {
resolve(fields)
})
// This was the bastard!
busboy.write(Buffer.from(event.body, 'base64'))
})
}
exports.handler = async function (event, context) {
try {
// Get url param
const url = event.queryStringParameters.url
// Parse multipart form for image
const { image } = await parseMultipartForm(event)
// Create a new form to send to Cloudflare Images and append our image
const data = new FormData()
data.append('file', image.content, image.filename.filename)
// Upload to cloudflare
const upload = await axios({
method: 'post',
url: `https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT_ID}/images/v1`,
headers: {
Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
...data.getHeaders(),
},
data
})
.then((response) => response.data)
.catch((error) => error)
return {
statusCode: 200,
body: JSON.stringify({ url, upload }),
}
} catch (error) {
console.error(error)
return {
statusCode: 500,
body: JSON.stringify({ error }),
}
}
}