Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@aws-sdk/client-s3 RequestTimeout error in PutObjectCommand #6762

Closed
4 tasks done
somnathPrasad opened this issue Dec 30, 2024 · 6 comments
Closed
4 tasks done

@aws-sdk/client-s3 RequestTimeout error in PutObjectCommand #6762

somnathPrasad opened this issue Dec 30, 2024 · 6 comments
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. potential-regression Marking this issue as a potential regression to be checked by team member

Comments

@somnathPrasad
Copy link

Checkboxes for prior research

Describe the bug

I am trying to upload an image file to a public s3 bucket. But every time I get this error, even when trying to upload a very small file (eg: 130kb)

RequestTimeout: Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.

This is the code block

const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
const config = require("../../config/config");

const { FILEUPLOADERROR, UPLOAD_SUCCESS } = config.response;
const { accessKeyId, region, secretAccessKey, public_bucket } =
  config.aws_config;

const s3Client = new S3Client({
  credentials: { accessKeyId, secretAccessKey },
  region,
});

module.exports = {
  uploadFilePublic: async (folderName, fileName, file) => {
    try {
      const date = new Date().valueOf();
      const fileType = file.mimetype.split("/")[1];
      const key = `${folderName}/${fileName}_${date}.${fileType}`;
      const downloadUrl = `https://${public_bucket}.s3.${region}.amazonaws.com/${key}`;

      const params = {
        Bucket: public_bucket,
        Key: key,
        Body: file.data,
      };

      const command = new PutObjectCommand(params);
      await s3Client.send(command);

      return {
        status: true,
        message: UPLOAD_SUCCESS,
        result: {
          name: file.name,
          type: file.mimetype,
          url: downloadUrl,
        },
      };
    } catch (error) {
      console.log(FILEUPLOADERROR, error);
      return { status: false, message: FILEUPLOADERROR };
    }
  },
};

But this is working in the old aws package "aws-sdk": "^2.962.0"

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/[email protected]

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v20.17.0

Reproduction Steps

The same bug can be reproduced through the above code.

Observed Behavior

I am getting this error.

RequestTimeout: Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.
    at throwDefaultError (D:\appstone\imgrows_v2\backend\node_modules\@aws-sdk\client-s3\node_modules\@smithy\smithy-client\dist-cjs\index.js:835:20)
    at D:\appstone\imgrows_v2\backend\node_modules\@aws-sdk\client-s3\node_modules\@smithy\smithy-client\dist-cjs\index.js:844:5
    at de_CommandError (D:\appstone\imgrows_v2\backend\node_modules\@aws-sdk\client-s3\dist-cjs\index.js:4919:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async D:\appstone\imgrows_v2\backend\node_modules\@aws-sdk\client-s3\node_modules\@smithy\middleware-serde\dist-cjs\index.js:35:20
    at async D:\appstone\imgrows_v2\backend\node_modules\@aws-sdk\middleware-sdk-s3\dist-cjs\index.js:485:18
    at async D:\appstone\imgrows_v2\backend\node_modules\@aws-sdk\client-s3\node_modules\@smithy\middleware-retry\dist-cjs\index.js:320:38
    at async D:\appstone\imgrows_v2\backend\node_modules\@aws-sdk\middleware-flexible-checksums\dist-cjs\index.js:286:18
    at async D:\appstone\imgrows_v2\backend\node_modules\@aws-sdk\middleware-sdk-s3\dist-cjs\index.js:110:22
    at async D:\appstone\imgrows_v2\backend\node_modules\@aws-sdk\middleware-sdk-s3\dist-cjs\index.js:138:14
    at async D:\appstone\imgrows_v2\backend\node_modules\@aws-sdk\client-s3\node_modules\@aws-sdk\middleware-logger\dist-cjs\index.js:34:22
    at async Object.uploadFilePublic (D:\appstone\imgrows_v2\backend\src\modules\v3\file-upload-module.js:29:7)
    at async Object.updateUserV2 (D:\appstone\imgrows_v2\backend\src\modules\v3\user-module.js:514:36)
    at async D:\appstone\imgrows_v2\backend\src\routes\v3\user-route.js:198:22 {
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 400,
    requestId: 'WCVYSEKRWW9G3E6Z',
    extendedRequestId: 'ceYIY8UWyWWZqU3QTW3QmM2CNJBvby7BGpSz6yoQ5kZmK6Zx+cvieiG4LeEHIiwUAFUDzJM1bR0=',
    cfId: undefined,
    attempts: 3,
    totalRetryDelay: 116
  },
  Code: 'RequestTimeout',
  RequestId: 'WCVYSEKRWW9G3E6Z',
  HostId: 'ceYIY8UWyWWZqU3QTW3QmM2CNJBvby7BGpSz6yoQ5kZmK6Zx+cvieiG4LeEHIiwUAFUDzJM1bR0='
}

Expected Behavior

I expected it to upload file to s3 and not give any errors

Possible Solution

No response

Additional Information/Context

No response

@somnathPrasad somnathPrasad added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 30, 2024
@github-actions github-actions bot added the potential-regression Marking this issue as a potential regression to be checked by team member label Dec 30, 2024
@jeffbski-rga
Copy link

@somnathPrasad I was recently bit by this too. Apparently the default socketTimeout is not sufficient since network traffic or when S3 is busy a RequestTimeout can occur. In our testing, the default SDK client timeout is less then 72s and can easily cause the RequestTimeout.

To fix this you simply need to increase the socketTimeout in the S3 client.

For example adding it to your code is as follows:

const s3Client = new S3Client({
  credentials: { accessKeyId, secretAccessKey },
  region,
  requestHandler: {
    socketTimeout: 5 * 60 * 1000 // ms (increase to 5m or other appropriate value)
  }
});
...

For reference see these links:

I am in the process of writing up an issue for the team to hopefully improve the examples and docs to help prevent this issue for others. When it is ready, I will link to this issue as well for reference.

@somnathPrasad
Copy link
Author

Thank you so much! @jeffbski-rga I was recently migrating from v2 to v3 for one of our older projects and encountered this issue. You are right; the error message it throws is not sufficient at all.

@somnathPrasad
Copy link
Author

closing this issue now

@jeffbski-rga
Copy link

@somnathPrasad AWS SDK team informed me that we should use requestTimeout rather than socketTimeout. socketTimeout is deprecated. #6763 (comment)

So the new code would be like:

const s3Client = new S3Client({
  credentials: { accessKeyId, secretAccessKey },
  region,
  requestHandler: {
    requestTimeout: 5 * 60 * 1000 // ms (increase to 5m or other appropriate value)
  }
});
...

@somnathPrasad
Copy link
Author

okay, it makes more sense.

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. potential-regression Marking this issue as a potential regression to be checked by team member
Projects
None yet
Development

No branches or pull requests

2 participants