Skip to content

Instantly share code, notes, and snippets.

@ssv445
Created November 2, 2023 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssv445/4d5dbed70d9afa3350f4581e96ccfe49 to your computer and use it in GitHub Desktop.
Save ssv445/4d5dbed70d9afa3350f4581e96ccfe49 to your computer and use it in GitHub Desktop.
Uploading a file to AWS presignedUrl using streaming the given file as a Url
"use strict";
const axios = require("axios");
const https = require("https");
async function uploadFileToPresignedUrl(presignedUrl, fileUrl) {
const response = await axios({
method: "get",
url: fileUrl,
responseType: "stream",
httpsAgent: new https.Agent({ keepAlive: true }),
});
const fileStream = response.data;
const uploadResponse = await axios({
method: "put",
url: presignedUrl,
data: fileStream,
headers: {
"Content-Type": response.headers["content-type"],
"Content-Length": response.headers["content-length"],
},
httpsAgent: new https.Agent({ keepAlive: true }),
maxContentLength: 5 * 1024 * 1024 * 1024, // 1GB
maxBodyLength: Infinity,
});
return uploadResponse;
}
class UploadService {
constructor() {
this.uploadStream = this.uploadStream.bind(this);
}
async uploadStream(presignedUrl, fileUrl) {
try {
const response = await uploadFileToPresignedUrl(presignedUrl, fileUrl);
return true;
} catch (error) {
throw error;
}
}
}
module.exports = UploadService;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment