gpt4 book ai didi

node.js - AWS S3 Node.js SDK `NotImplemented` Multer 错误

转载 作者:行者123 更新时间:2023-12-05 01:06:51 26 4
gpt4 key购买 nike

我正在尝试设置一个端点以将文件上传到 AWS S3 存储桶。我正在使用 Node.JS、Express、AWS S3 SDK 和 Multer 库。

这是我目前用于上传中间件的代码:

export const uploadMiddleware = multer({
storage: {
_handleFile(
req: Request,
file: Express.Multer.File,
callback: (error?: any, info?: Partial<Express.Multer.File>) => void
) {
const key = crypto.randomBytes(16).toString('hex');
s3.send(
new PutObjectCommand({
Bucket: 'my-bucket',
Body: file.stream,
Key: key,
ContentLength: file.size,
ContentType: file.mimetype,
}),
(err, output) => {
if (err) {
console.log(err);
callback(err, undefined);
} else {
console.log(output);
callback(null, file);
}
}
);
},
_removeFile(
req: Request,
file: Express.Multer.File,
callback: (error: Error) => void
) {
// @ts-ignore
callback(null);
},
},
}).array('files');

但是,S3 SDK 给出了这个错误:

NotImplemented: A header you provided implies functionality that is not implemented
2021-07-11T00:18:48.557532998Z at deserializeAws_restXmlPutObjectCommandError (/usr/src/app/node_modules/@aws-sdk/client-s3/protocols/Aws_restXml.ts:9515:39)
2021-07-11T00:18:48.557542041Z at processTicksAndRejections (internal/process/task_queues.js:93:5)
2021-07-11T00:18:48.557545571Z at /usr/src/app/node_modules/@aws-sdk/middleware-serde/src/deserializerMiddleware.ts:18:20
2021-07-11T00:18:48.557548319Z at /usr/src/app/node_modules/@aws-sdk/middleware-signing/src/middleware.ts:26:22
2021-07-11T00:18:48.557551272Z at StandardRetryStrategy.retry (/usr/src/app/node_modules/@aws-sdk/middleware-retry/src/StandardRetryStrategy.ts:83:38)
2021-07-11T00:18:48.557554272Z at /usr/src/app/node_modules/@aws-sdk/middleware-logger/src/loggerMiddleware.ts:22:22 {
2021-07-11T00:18:48.557557035Z Code: 'NotImplemented',
2021-07-11T00:18:48.557559463Z Header: 'Transfer-Encoding',
2021-07-11T00:18:48.557561825Z RequestId: 'T6QNENNWC0X2WDXP',
2021-07-11T00:18:48.557564202Z HostId: 'gJo7gOiFu9RjdvishH0t6EP8/BHDlBW913gicSlYh1eyJ/JkZaX9QSepRmdeq5Gxt4lg6aKMxxI=',
2021-07-11T00:18:48.557566724Z '$fault': 'client',
2021-07-11T00:18:48.557569116Z '$metadata': {
2021-07-11T00:18:48.557571528Z httpStatusCode: 501,
2021-07-11T00:18:48.557573948Z requestId: undefined,
2021-07-11T00:18:48.557576323Z extendedRequestId: 'gJo7gOiFu9RjdvishH0t6EP8/BHDlBW913gicSlYh1eyJ/JkZaX9QSepRmdeq5Gxt4lg6aKMxxI=',
2021-07-11T00:18:48.557578821Z cfId: undefined,
2021-07-11T00:18:48.557589903Z attempts: 1,
2021-07-11T00:18:48.557592818Z totalRetryDelay: 0
2021-07-11T00:18:48.557595524Z },
2021-07-11T00:18:48.557597888Z storageErrors: []
2021-07-11T00:18:48.557600254Z }

我尝试了多种可能的解决方案,例如包括/排除 ContentLengthContentType,以及使用 file.buffer 而不是 文件流。我还尝试了相关 StackOverflow 帖子中的其他解决方案,但没有任何效果。

请注意,我也知道存在用于 Multer/S3 互操作性的 NPM 包,但是没有一个使用新的模块化 AWS 系统,因此我选择使用自己的。

任何帮助将不胜感激,谢谢!

最佳答案

我必须在 handleFile 函数中将流转换为缓冲区:

async function streamToBuffer(stream: Stream): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
const _buf: any[] = [];

stream.on('data', (chunk) => _buf.push(chunk));
stream.on('end', () => resolve(Buffer.concat(_buf)));
stream.on('error', (err) => reject(err));
});
}

export function handleFile(
req: Request,
file: Express.Multer.File,
callback: (error?: any, info?: Express.Multer.File) => void
) {
const key = generateId();

streamToBuffer(file.stream)
.then((buffer) =>
s3.send(
new PutObjectCommand({
Bucket: config.aws.issueNewBucket,
Body: buffer,
Key: key,
ACL: 'public-read',
ContentType: file.mimetype,
})
)
)
.then(() => callback(null, file))
.catch((err) => callback(err));
}

关于node.js - AWS S3 Node.js SDK `NotImplemented` Multer 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68332633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com