首页 > 解决方案 > 未捕获的错误:超过了 2097152 字节的 DelayedStream#maxDataSize

问题描述

我想将文件上传到 s3 但不等待它但是我收到以下错误。

未捕获的错误:超过了 2097152 字节的 DelayedStream#maxDataSize。在 FormData.CombinedStream._checkDataSize (node_modules/combined-stream/lib/combined_stream.js:166:19) 在 Readable.EventEmitter.emit (domain.js:476:20) 在 Readable.source.emit (node_modules/delayed-stream /lib/delayed_stream.js:30:21) 在 Readable.read (_stream_readable.js:520:10) 在 ManagedUpload.fillStream (node_modules/aws-sdk/lib/s3/managed_upload.js:422:25) 在 Readable。(node_modules/aws-sdk/lib/s3/managed_upload.js:188:44) 在 Readable.EventEmitter.emit (domain.js:476:20) 在 Readable.source.emit (node_modules/delayed-stream/lib/delayed_stream .js:30:21) 在emitReadable_ (_stream_readable.js:570:12) 在 processTicksAndRejections (internal/process/task_queues.js:77:11)

用法

console.log(image)
this.storageModule.upload(`${requestId}-${type}.jpg`, image)
> Readable {
>   _readableState: ReadableState {
>     objectMode: false,
>     highWaterMark: 16384,
>     buffer: BufferList { head: null, tail: null, length: 0 },
>     length: 0,
>     pipes: null,
>     pipesCount: 0,
>     flowing: null,
>     ended: false,
>     endEmitted: false,
>     reading: false,
>     sync: true,
>     needReadable: false,
>     emittedReadable: false,
>     readableListening: false,
>     resumeScheduled: false,
>     paused: true,
>     emitClose: true,
>     autoDestroy: false,
>     destroyed: false,
>     defaultEncoding: 'utf8',
>     awaitDrain: 0,
>     readingMore: false,
>     decoder: null,
>     encoding: null
>   },
>   readable: true,
>   _read: [Function: read],
>   _events: [Object: null prototype] {},
>   _eventsCount: 0,
>   _maxListeners: undefined
> }

存储模块

import Config from "../../config/config";
import S3 from "aws-sdk/clients/s3";

class StorageModule {

    public config: Config;
    public s3: S3;
    public bucket: string;

    constructor() {
        this.config = new Config();

            const credentials: S3.ClientConfiguration = {
                accessKeyId: this.config.aws.AWS_S3_ACCESS_KEY_ID,
                secretAccessKey: this.config.aws.AWS_S3_SECRET_ACCESS_KEY,
                region: this.config.aws.AWS_S3_REGION,
            };

            this.s3 = new S3(credentials);
            this.bucket = "test-bucket-name";

        }

    }

    public upload = (key, body) => {

        const params = {
            Bucket: this.bucket,
            Key: key,
            Body: body
        }

        return this.s3.upload(params).promise()

    }

}

export default StorageModule;

但是,当我从测试运行上传时,它可以工作。

import StorageModule from "./storage.module";
import fs from "fs";
import { expect } from "chai";

describe("StorageModule", () => {

    it("should upload file to s3", async () => {

        const storageModule = new StorageModule();

        const stream = await fs.createReadStream(process.cwd() + "/test/resources/" + "id/front.jpg");

        console.log(stream);

        const key = "test/productId/sessionId/requestId-front.jpg"

        const response = await storageModule.upload(key, stream);

        expect(response.Location).to.equal(`${base}/${key}`)

    });

});

它与可读流有关吗,因为我尝试上传的图像完全相同?

标签: node.jsexpressaws-sdkaws-sdk-jsaws-sdk-nodejs

解决方案


推荐阅读