Created
May 28, 2022 14:18
-
-
Save samuelko123/c5d1042575dd40e8fb48162f20c3d0ff to your computer and use it in GitHub Desktop.
Bulk upload to ImageKit
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 axios = require('axios') | |
const fs = require('fs') | |
const path = require('path') | |
async function upload(image_url_or_base64, file_name, overwrite) { | |
const data = { | |
file: image_url_or_base64, | |
fileName: file_name, | |
useUniqueFileName: !overwrite, | |
overwriteFile: overwrite, | |
folder: `production/time_signatures`, | |
} | |
const form = new FormData() | |
for (const key in data) { | |
form.append(key, data[key].toString()) | |
} | |
const url = 'https://upload.imagekit.io/api/v1/files/upload' | |
const request = { | |
url: url, | |
method: 'POST', | |
data: form, | |
headers: { | |
Authorization: 'Basic ' + Buffer.from(`<ImageKit PrivateKey>:`).toString('base64'), | |
'Content-Type': `multipart/form-data; boundary=${form._boundary}`, | |
Accept: 'application/json', | |
}, | |
} | |
const res = await axios.request(request) | |
return res.data.url | |
} | |
const files = [ | |
'C:\\a.png', | |
] | |
files.forEach(file_path => { | |
const base64 = 'data:image/png;base64,' + fs.readFileSync(file_path, { encoding: 'base64' }) | |
const file_name = path.basename(file_path).replace('.png', '') | |
upload(base64, file_name, true) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment