lewisdale.dev/scripts/upload-to-bunny.js

34 lines
922 B
JavaScript
Raw Permalink Normal View History

2024-11-11 15:45:36 +00:00
import pMap from 'p-map';
import { readdir, lstat } from 'node:fs/promises';
import { createReadStream } from 'node:fs';
import * as path from 'node:path';
const config = {
endpoint: process.env.BUNNY_STORAGE_ENDPOINT,
password: process.env.BUNNY_STORAGE_PASSWORD,
bucket: process.env.BUNNY_STORAGE_BUCKET
};
const files = await readdir('./_site', { recursive: true });
await pMap(files, async (file) => {
const filePath = path.join('./_site', file);
const stat = await lstat(filePath);
if (stat.isDirectory()) return;
const url = `https://${config.endpoint}/${config.bucket}/${file}`;
console.log(`Uploading ${filePath} to ${url}`);
const readStream = createReadStream(filePath);
await fetch(url, {
method: 'PUT',
headers: {
accessKey: config.password,
},
body: readStream,
duplex: 'half'
});
}, { concurrency: 50 });