Created
November 2, 2023 05:17
-
-
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
This file contains 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
"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