From c933cc5194e48baba54a00f765509eb652760679 Mon Sep 17 00:00:00 2001 From: Rohit Amarnath <88762+ramarnat@users.noreply.github.com> Date: Tue, 23 Sep 2025 14:14:54 -0400 Subject: [PATCH] fix: update upload handling in uploads.ts for AWS S3 compatibility - Changed response type from 'stream' to 'arraybuffer' in downloadFromUrl to accommodate different data handling. - Added logic to include 'Content-Length' header for AWS S3 uploads to prevent 501 Transfer-Encoding errors. This update enhances the upload functionality by ensuring compatibility with AWS S3 requirements, improving the reliability of file uploads. --- packages/api/src/utils/uploads.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/api/src/utils/uploads.ts b/packages/api/src/utils/uploads.ts index a9d6a0426..d9319ea82 100644 --- a/packages/api/src/utils/uploads.ts +++ b/packages/api/src/utils/uploads.ts @@ -109,7 +109,7 @@ export const downloadFromUrl = async ( ) => { // download the content as stream and max 10MB const response = await axios.get(contentObjUrl, { - responseType: 'stream', + responseType: 'arraybuffer', maxContentLength, timeout, }) @@ -123,11 +123,21 @@ export const uploadToSignedUrl = async ( contentType: string, timeout?: number ) => { + // Check if this is an AWS S3 URL (requires Content-Length header) + const isAwsS3 = uploadSignedUrl.includes('amazonaws.com') + + const headers: Record = { + 'Content-Type': contentType, + } + + // AWS S3 requires Content-Length header to avoid 501 Transfer-Encoding error + if (isAwsS3) { + headers['Content-Length'] = data.length + } + // upload the stream to the signed url await axios.put(uploadSignedUrl, data, { - headers: { - 'Content-Type': contentType, - }, + headers, maxBodyLength: maxContentLength, timeout, })