Skip to content

Unknown Error 400 #6879

@hasnainmalik-siar

Description

@hasnainmalik-siar

Describe the issue

Here is my code for uploading a file to S3. However, on the first attempt, it returns a 400 Unknown Error, and the full error log displays body: '\nYour browser sent an invalid request.\n'. Please suggest a way to fix it,

import { S3Client, S3 } from '@aws-sdk/client-s3';
import { Upload } from "@aws-sdk/lib-storage";
import dotenv from "dotenv";
import logger from '../config/logger.js';
dotenv.config();

const s3 = new S3({
  endpoint: process.env.DO_SPACES_ENDPOINT,
  forcePathStyle: true,
  region: "sfo3",
  credentials: {
    accessKeyId: process.env.DO_SPACES_KEY,
    secretAccessKey: process.env.DO_SPACES_SECRET,
  },
  logger: console, // Enable logging
});


export async function uploadExportFileToS3(key, mimeType, file, id) {
  try {
    const params = {
      Bucket: "exportify",
      Key: key,
      Body: file,
      ContentType: mimeType,
      ContentLength: file.length,
    };

    // Perform parallel upload
    const parallelUpload = new Upload({
      client: s3,
      params,
      queueSize: 3,
    });

    parallelUpload.on('httpUploadProgress', (progress) => {
      console.log("This is progress :: ", progress);
    });

    const resp = await parallelUpload.done();
    console.log("resp :: ", resp)
    return { location: resp.Location, id };

  } catch (error) {
    if (error instanceof Error && error.name === "AbortError") {
      logger.error(`Multipart upload was aborted. ${error.message}`);
    } else {
      console.log("Error uploading file:", error.message);
      logger.error("Error uploading file:" + error.message)
      // throw error;
    }
  }
}

This is full error log

error: 400: UnknownError
16:12:29 │ web-backend  │       at throwDefaultError (D:\Hasnain Projects\sairify\node_modules\@smithy\smithy-client\dist-cjs\index.js:849:20)
16:12:29 │ web-backend  │       at D:\Hasnain Projects\sairify\node_modules\@smithy\smithy-client\dist-cjs\index.js:858:5
16:12:29 │ web-backend  │       at de_CommandError (D:\Hasnain Projects\sairify\node_modules\@aws-sdk\client-s3\dist-cjs\index.js:4935:14)
16:12:29 │ web-backend  │       at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
16:12:29 │ web-backend  │       at async D:\Hasnain Projects\sairify\node_modules\@smithy\middleware-serde\dist-cjs\index.js:35:20
16:12:29 │ web-backend  │       at async D:\Hasnain Projects\sairify\node_modules\@aws-sdk\middleware-sdk-s3\dist-cjs\index.js:483:18
16:12:29 │ web-backend  │       at async D:\Hasnain Projects\sairify\node_modules\@smithy\middleware-retry\dist-cjs\index.js:321:38
16:12:29 │ web-backend  │       at async D:\Hasnain Projects\sairify\node_modules\@aws-sdk\middleware-flexible-checksums\dist-cjs\index.js:286:18
16:12:29 │ web-backend  │       at async D:\Hasnain Projects\sairify\node_modules\@aws-sdk\middleware-sdk-s3\dist-cjs\index.js:109:22
16:12:29 │ web-backend  │       at async D:\Hasnain Projects\sairify\node_modules\@aws-sdk\middleware-sdk-s3\dist-cjs\index.js:136:14
16:12:29 │ web-backend  │       at async D:\Hasnain Projects\sairify\node_modules\@aws-sdk\middleware-logger\dist-cjs\index.js:33:22
16:12:29 │ web-backend  │       at async Promise.all (index 0)
16:12:29 │ web-backend  │       at async _Upload.__uploadUsingPut (D:\Hasnain Projects\sairify\node_modules\@aws-sdk\lib-storage\dist-cjs\index.js:260:22)   
16:12:29 │ web-backend  │       at async _Upload.__doConcurrentUpload (D:\Hasnain
Projects\sairify\node_modules\@aws-sdk\lib-storage\dist-cjs\index.js:330:16)
16:12:29 │ web-backend  │       at async Promise.all (index 0)
16:12:29 │ web-backend  │       at async _Upload.__doMultipartUpload (D:\Hasnain Projects\sairify\node_modules\@aws-sdk\lib-storage\dist-cjs\index.js:415:5) 
{
16:12:29 │ web-backend  │     '$fault': 'client',
16:12:29 │ web-backend  │     '$metadata': {
16:12:29 │ web-backend  │       httpStatusCode: 400,
16:12:29 │ web-backend  │       requestId: undefined,
16:12:29 │ web-backend  │       extendedRequestId: undefined,
16:12:29 │ web-backend  │       cfId: undefined,
16:12:29 │ web-backend  │       attempts: 1,
16:12:29 │ web-backend  │       totalRetryDelay: 0
16:12:29 │ web-backend  │     },
16:12:29 │ web-backend  │     body: '\nYour browser sent an invalid request.\n'
16:12:29 │ web-backend  │   },
16:12:29 │ web-backend  │   metadata: {
16:12:29 │ web-backend  │     httpStatusCode: 400,
16:12:29 │ web-backend  │     requestId: undefined,
16:12:29 │ web-backend  │     extendedRequestId: undefined,
16:12:29 │ web-backend  │     cfId: undefined,
16:12:29 │ web-backend  │     attempts: 1,
16:12:29 │ web-backend  │     totalRetryDelay: 0
16:12:29 │ web-backend  │   }
16:12:29 │ web-backend  │ }`

Links

https://github.com/aws/aws-sdk-js-v3/blob/main/lib/lib-storage/example-code/file-upload.ts

Metadata

Metadata

Assignees

Labels

closed-for-stalenessdocumentationThis is a problem with documentation.p2This is a standard priority issueresponse-requestedWaiting on additional info and feedback. Will move to \"closing-soon\" in 7 days.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions