0

Node version : node -v v20.17.0

packages versions : "@types/aws-lambda": "^8.10.145", "@aws-sdk/client-s3": "^3.678.0", "file-type": "^19.6.0"

When getting an object from s3 bucket, the stream returned is not compatible with the stream accepted by the fileTypeFromStream method :

import { GetObjectCommand, S3Client, type GetObjectCommandInput } from '@aws-sdk/client-s3';
import type { Handler, S3Event } from 'aws-lambda';
import { fileTypeFromStream } from 'file-type';

export const handler: Handler = async (event: S3Event) => {
  const record = event.Records[0];

  const input: GetObjectCommandInput = {
    Bucket: record.s3.bucket.name,
    Key: record.s3.object.key,
  };

  const command = new GetObjectCommand(input);

  const s3Client = new S3Client();

  const body = (await s3Client.send(command)).Body;

  if (body) {
    await fileTypeFromStream(body);
  }
};

I'm getting the following error : enter image description here

Do you know have an idea of whats is the issue?

EDIT :

I've called the method transformToWebStream on the Body, to bypass the compilation error, but this time I'm getting an issue at runtime :

{
    "errorType": "TypeError",
    "errorMessage": "The argument 'stream' must be a byte stream. Received ReadableStream { locked: false, state: 'readable', supportsBYOB: false }",
    "code": "ERR_INVALID_ARG_VALUE",
    "stack": [
        "TypeError [ERR_INVALID_ARG_VALUE]: The argument 'stream' must be a byte stream. Received ReadableStream { locked: false, state: 'readable', supportsBYOB: false }",
        "    at new NodeError (node:internal/errors:405:5)",
        "    at setupReadableStreamBYOBReader (node:internal/webstreams/readablestream:2146:11)",
        "    at new ReadableStreamBYOBReader (node:internal/webstreams/readablestream:918:5)",
        "    at ReadableStream.getReader (node:internal/webstreams/readablestream:355:12)",
        "    at new kl (/var/task/index.js:56:259360)",
        "    at Ap (/var/task/index.js:56:262891)",
        "    at SS.fromStream (/var/task/index.js:57:7228)",
        "    at sG (/var/task/index.js:57:7930)",
        "    at Runtime.lbe [as handler] (/var/task/index.js:57:8141)",
        "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
    ]
} 
2
  • You're almost there. The library you use expects: ReadableStream<Uint8Array>; while you are passing ReadableStream (which defaults to chunks of js objects). Convert the stream you are passing into Uint8Array data and send it to the file-type function Commented Oct 24, 2024 at 10:51
  • thanks, could you detail a bit more your answer please
    – bssyy78
    Commented Oct 24, 2024 at 11:43

1 Answer 1

0

Let's convert the body to a ReadableStream object. In this case, let use transformToWebStream() function:

  if (body) {
    await fileTypeFromStream(body.transformToWebStream());
  }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.