UNPKG

genezio

Version:

Command line utility to interact with Genezio infrastructure.

56 lines (55 loc) 1.8 kB
import fs from "fs"; import { getAuthToken } from "../utils/accounts.js"; import https from "https"; import { GENEZIO_NOT_AUTH_ERROR_MSG, UserError } from "../errors.js"; export async function uploadContentToS3(presignedURL, archivePath, progress, userId) { if (!presignedURL) { throw new UserError("Missing presigned URL"); } if (!archivePath) { throw new UserError("Missing required parameters"); } // Check if user is authenticated const authToken = await getAuthToken(); if (!authToken) { throw new UserError(GENEZIO_NOT_AUTH_ERROR_MSG); } const url = new URL(presignedURL); const headers = { "Content-Type": "application/octet-stream", "Content-Length": fs.statSync(archivePath).size, }; if (userId) { headers["x-amz-meta-userid"] = userId; } const options = { hostname: url.hostname, path: url.href, port: 443, method: "PUT", headers: headers, }; return await new Promise((resolve, reject) => { const req = https.request(options, (res) => { // If we don't consume the data, the "end" event will not fire // eslint-disable-next-line @typescript-eslint/no-empty-function res.on("data", () => { }); res.on("end", () => { resolve(); }); }); req.on("error", (error) => { reject(error); }); const { size } = fs.statSync(archivePath); const fileStream = fs.createReadStream(archivePath); let total = 0; fileStream .on("data", (data) => { total += data.length; if (progress) progress(total / size); }) .pipe(req); }); }