gpt4 book ai didi

node.js - 为什么 s3.putObject 会为 nodejs Buffer 抛出 ERR_INVALID_ARG_TYPE ?

转载 作者:太空宇宙 更新时间:2023-11-03 23:55:01 25 4
gpt4 key购买 nike

我正在nodejs中编写一个lambda函数,用于将图像上传到S3存储桶。

在我的第一个 lambda 函数中,我已经内联编写了这个函数(直接在 AWS Lambda 编辑器中),并且它运行得非常好。相关代码为:

let decodedImage = Buffer.from(encodedImage, 'base64');
console.log(decodedImage);
console.log(typeof decodedImage);
console.log(Object.prototype.toString.call(decodedImage));

try {
await writeToBucket(decodedImage, 'test.png', 'image/png');
} catch (err) {
console.log(err);
}

async function writeToBucket(data, dbFileId, mimeType) {
return new Promise((resolve, reject) => {

var s3 = new AWS.S3();
const params = {
Bucket : S3_BUCKET,
Key : dbFileId,
Body : data,
ACL: 'public-read',
ContentType: mimeType
}

s3.putObject(params, function(err, dataString) {
if (err) {
reject([503, err]);
} else {
resolve();
}
});
});

三个日志给出:

<Buffer 89 50 4e 47 ... >
object
[object UInt8Array]

因此,如上所述,上述工作正常并且图像已写入存储桶。

现在,我有第二个 Lambda 函数,我在本地编写该函数,并在使用 webpack 构建、压缩和上传后上传到 Lambda。两个 lambda 中的代码相同(如上所述),并且控制台输出也相同。但在第二种情况下上传失败,并出现以下错误:

{ NetworkingError: The "chunk" argument must be one of type string or Buffer. Received type object
at ClientRequest.end (_http_outgoing.js:690:13)
at features.constructor.writeBody (/var/runtime/node_modules/aws-sdk/lib/http/node.js:137:14)
at features.constructor.handleRequest (/var/runtime/node_modules/aws-sdk/lib/http/node.js:105:12)
at executeSend (/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:342:29)
at Request.SEND (/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:356:9)
at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:102:18)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)
message:
'The "chunk" argument must be one of type string or Buffer. Received type object',
code: 'NetworkingError',
region: 'eu-west-1',
hostname: 's3.eu-west-1.amazonaws.com',
retryable: true,
time: 2019-08-26T14:25:23.006Z }

据我所知,Body 有效负载确实是 Buffer 类型,但我不明白有什么区别?会不会是 Webpack(或 Babel)搞乱了某些东西?或者它可能与 Nodejs.Buffer 有关吗?我在两个 Lambda 中运行 Node.js 10.x。

我的 Webpack 设置是这样的:

const path = require('path');
require("@babel/register");
require("@babel/plugin-transform-runtime");

const config = {
mode: 'development',
entry: './index.js',

output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
library: 'index',
libraryTarget: 'commonjs2'
},
// Loaders
module: {
rules : [
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" },
{ test: /\.css$/, use: ['style-loader', 'css-loader']}
]
},
// Plugins
plugins: [],
optimization:{
minimize: false
},
externals: {
'aws-sdk': 'aws-sdk'
}
};
// Exports
module.exports = config;

和 package.json 依赖项:

"dependencies": {
"@babel/register": "~7.5.5",
"@babel/runtime": "~7.5.5",
"ajv": "~6.10.2",
"ajv-errors": "~1.0.1",
"css-loader": "~3.2.0",
"style-loader": "~1.0.0",
"uuid": "~3.3.3"
},
"devDependencies": {
"@babel/cli": "~7.5.5",
"@babel/core": "~7.5.5",
"@babel/node": "~7.5.5",
"@babel/plugin-transform-runtime": "~7.5.5",
"@babel/preset-env": "~7.5.5",
"aws-sdk": "~2.517.0",
"babel-loader": "~8.0.6",
"cross-var-no-babel": "~1.2.0",
"mocha": "^6.1.4",
"webpack": "~4.33.0",
"webpack-cli": "~3.3"
}

最佳答案

此错误令人困惑,因为您的代码确实使用 Buffer 作为 Body 并且没有任何可见的 chunk 参数,因此必须是SDK 内部。单独而言,假设 SDK 未正确处理某些内容,并且抛出异常的时间比预期晚得多(例如,不是调用 s3.putObject() 的直接结果,而是稍后,在实际的“工作”开始之后)。

我有一个挥之不去的想法,我在尝试优化其大小以进行分发时读到过有关 AWS SDK 的问题...但我找不到我要找的东西,而且您在 中有它外部所以也许这是不相关的。

但我确实找到了Bundling Applications with Webpack在哪里SDK v2 的 AWS SDK for JavaScript 开发人员指南中提到了一些可能相关的内容:

You can use webpack to generate bundles that run in Node.js by specifying it as a target in the configuration.

target: "node"

webpack 文档提到这个 "...will compile for usage in a Node.js-like environment (uses Node.js require to load chunks and not touch any built in modules like fs or path)" 。我不清楚这个解释是否包含一个隐藏的金 block ,解释了为什么不包含它会破坏 SDK,或者是否还有其他事情发生......但指定这似乎是解决方案。

如果我推测,我的猜测是,这是通过禁用由 webpack 添加的 Buffer polyfill 来实现的。

关于node.js - 为什么 s3.putObject 会为 nodejs Buffer 抛出 ERR_INVALID_ARG_TYPE ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57660511/

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