This commit is contained in:
2026-01-26 15:53:24 +01:00
parent 97f5b6d7d9
commit e090c57746
2 changed files with 46 additions and 12 deletions
+38
View File
@@ -0,0 +1,38 @@
import { S3Client } from "bun";
import { createWriteStream } from "fs";
import path from "path";
type Params = {
downloadPath: string;
downloadFileName: string;
folder?: string;
};
const client = new S3Client({
accessKeyId: process.env.CLOUDFLARE_R2_ACCESS_KEY_ID,
secretAccessKey: process.env.CLOUDFLARE_R2_SECRET,
bucket: "coderank",
endpoint: process.env.CLOUDFLARE_R2_ENDPOINT,
});
export default async function s3DownloadFile({
downloadPath,
folder,
downloadFileName,
}: Params) {
let finalFileName = path.join(folder || "", downloadFileName);
const s3file = client.file(finalFileName);
const writable = createWriteStream(downloadPath);
const reader = s3file.stream().getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
writable.write(value);
}
writable.end();
console.log("Download complete!");
}