Skip to content

[client-s3] client is not sending chunks read from fs.ReadSteam when ChecksumAlgorithm is set #3368

Closed
@trivikr

Description

@trivikr

Describe the bug

client is not sending chunks read from fs.ReadSteam when ChecksumAlgorithm is set

Your environment

SDK version number

@aws-sdk/[email protected]

Is the issue in the browser/Node.js/ReactNative?

Node.js

Details of the browser/Node.js/ReactNative version

$ node -v
v16.13.2

Steps to reproduce

import { S3 } from "@aws-sdk/client-s3"; // v3.53.0

import { createReadStream } from "fs";

const logRequestMiddleware = (next) => async (args) => {
  const { body } = args.request;

  let chunkCount = 0;
  body.on("data", (chunk) => {
    console.log(`actual sent chunk #${chunkCount}: '${chunk.toString()}'`);
    chunkCount++;
  });

  return next(args);
};
const logRequestMiddlewareOptions = { step: "deserialize" };

const region = "us-west-2";
const client = new S3({ region });
client.middlewareStack.add(logRequestMiddleware, logRequestMiddlewareOptions);

const fileName = "hello-world.txt"; // File contains "helloworld"
// Because of highWaterMark=5, data will be read in two chunks: 'hello', 'world'
const readableStream = createReadStream(fileName, { highWaterMark: 5 });

let chunkCount = 0;
readableStream.on("data", (chunk) => {
  console.log(`stream sent chunk #${chunkCount}: '${chunk.toString()}'`);
  chunkCount++;
});

const Bucket = "aws-sdk-js-trivikr-flexible-checksums-testing"; // replace with your bucket
const Key = fileName;
const Body = readableStream;
const ChecksumAlgorithm = "CRC32";

try {
  await client.putObject({ Bucket, Key, Body, ChecksumAlgorithm });
} catch (error) {
  console.log({ error });
}

Observed behavior

The data from file is not sent over the wire.

stream sent chunk #0: 'hello'
stream sent chunk #1: 'world'
actual sent chunk #0: '0
'
actual sent chunk #1: 'x-amz-checksum-crc32:AAAAAA==
'
actual sent chunk #2: '
'
{
  error: IncompleteBody: The request body terminated unexpectedly
      at deserializeAws_restXmlPutObjectCommandError (/local/home/trivikr/workspace/temp/node_modules/@aws-sdk/client-s3/dist-cjs/protocols/Aws_restXml.js:8037:24)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async /local/home/trivikr/workspace/temp/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:7:24
      at async /local/home/trivikr/workspace/temp/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/middleware-signing/dist-cjs/middleware.js:11:20
      at async StandardRetryStrategy.retry (/local/home/trivikr/workspace/temp/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/middleware-retry/dist-cjs/StandardRetryStrategy.js:51:46)
      at async /local/home/trivikr/workspace/temp/node_modules/@aws-sdk/middleware-flexible-checksums/dist-cjs/flexibleChecksumsMiddleware.js:56:20
      at async /local/home/trivikr/workspace/temp/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:6:22
      at async file:///local/home/trivikr/workspace/temp/putObject.brawn.mjs:38:3 {
    '$fault': 'client',
    '$metadata': {
      httpStatusCode: 400,
      requestId: undefined,
      extendedRequestId: '9XGOJ07zW1IHBjTtAyE/+Mv97SzhwzNKTiibWfaSEr0dAP1KPbB4Y44K1Yhh0rfL4xup8M8+35I=',
      cfId: undefined,
      attempts: 1,
      totalRetryDelay: 0
    },
    Code: 'IncompleteBody',
    RequestId: 'A0Z1NB2HD491JASB',
    HostId: '9XGOJ07zW1IHBjTtAyE/+Mv97SzhwzNKTiibWfaSEr0dAP1KPbB4Y44K1Yhh0rfL4xup8M8+35I=',
    '$response': HttpResponse {
      statusCode: 400,
      headers: [Object],
      body: [IncomingMessage]
    }
  }
}

Expected behavior

The request should be successful, and the output should print.

stream sent chunk #0: 'hello'
stream sent chunk #1: 'world'
actual sent chunk #0: '5
hello
'
actual sent chunk #1: '5
world
'
actual sent chunk #2: '0
'
actual sent chunk #3: 'x-amz-checksum-crc32:AAAAAA==
'
actual sent chunk #4: '
'

Additional context

Tests for reference:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions