Node.js 使用AWS SDK上传节点JS文件|错误:无法确定[object]的长度
我正在尝试在我的sails js应用程序上使用AWS SDK。我经常遇到这样的错误:无法确定[object]的长度。有人知道这是什么原因吗 代码如下: var AWS=要求('AWS-sdk') 根据Node.js 使用AWS SDK上传节点JS文件|错误:无法确定[object]的长度,node.js,amazon-s3,sails.js,aws-sdk,Node.js,Amazon S3,Sails.js,Aws Sdk,我正在尝试在我的sails js应用程序上使用AWS SDK。我经常遇到这样的错误:无法确定[object]的长度。有人知道这是什么原因吗 代码如下: var AWS=要求('AWS-sdk') 根据 正文的值应为缓冲区、blob或流 upload(params = {}, [options], [callback]) 上载任意大小的缓冲区、blob或流,如果负载足够大,则使用智能并发处理部件。您可以通过设置选项来配置并发队列大小 您需要像fs.createReadStream(req.fil
正文
的值应为缓冲区、blob或流
upload(params = {}, [options], [callback])
上载任意大小的缓冲区、blob或流,如果负载足够大,则使用智能并发处理部件。您可以通过设置选项来配置并发队列大小
您需要像fs.createReadStream(req.file.path)
一样对文件进行流式处理,然后发送并将其放入正文
参数嗨,我遇到了同样的问题,
我把我的密码寄给你
var s3bucket = new AWS.S3({
accessKeyId: 'xxx',
secretAccessKey: 'xxx+xxx',
});
var body = new Buffer(req.file('cover'), 'base64');
var params = {
Bucket: 'sweetestspots',
Key: 'yourimagename',
Body: body,
ContentEncoding: 'base64',
ContentType: 'image/png',
ACL: 'public-read'
};
s3bucket.upload(params, function(err, data) {
if (err) {
console.log("Error uploading data: ", err);
} else {
console.log("Successfully uploaded data to myBucket" + JSON.stringify(data));
}
});
请尝试此代码。我也遇到这种错误。问题是您需要将
Body
作为Buffer
传递。我这样做了,问题就消失了:
router.post("/upload-image", (req, res) => {
let imageFileName;
const busboy = new BusBoy({ headers: req.headers });
busboy.on("file", (fieldName, file, fileName, encoding, mimetype) => {
if (mimetype !== "image/jpeg" && mimetype !== "image/png") {
return res.status(400).json({ error: "Bad format in the image" });
}
const filePath = path.join(os.tmpdir(), fileName);
const params = {
Bucket: "your bucket name",
Key: fileName,
Body: "",
ACL: "public-read",
ContentType: mimetype,
};
file.on("data", function (data) {
params.ContentLength = data.length;
params.Body = data;
});
// needed to call upload function after a while, because params was not updating immediately
setTimeout(() => {
s3.upload(params, (error, data) => {
if (error) {
return res.status(500).send(error);
}
return res.status(200).json({ imageURL: data.Location });
});
}, 1000);
file.pipe(fs.createWriteStream(filePath));
});
busboy.end(req.rawBody);
});
这段代码还将解决将图像上载到s3后获取零字节的问题我在使用putObject上载流时遇到了这个错误。解决方案是切换到上载,或者使用缓冲区/blob而不是流。我可以知道您正在上载的
body
的值吗?body是从前端解析的请求文件(“cover”),它是一个图像。不要显示您的密钥。谢谢@arjunkori:D已删除now@steph-你必须换钥匙。编辑这个问题是不够的。人们可以在历史上看到它们。现在就做。谢谢,我真的很感谢你的帮助。我现在得到一个关于缓冲区的错误。它说:TypeError:第一个参数必须是字符串、缓冲区、ArrayBuffer、数组或类似数组的对象;不要粘贴你的钥匙!(我会很快更改它们)将其更改为var body=newbuffer(req.file.cover,'base64);'
var s3bucket = new AWS.S3({
accessKeyId: 'xxx',
secretAccessKey: 'xxx+xxx',
});
var body = new Buffer(req.file('cover'), 'base64');
var params = {
Bucket: 'sweetestspots',
Key: 'yourimagename',
Body: body,
ContentEncoding: 'base64',
ContentType: 'image/png',
ACL: 'public-read'
};
s3bucket.upload(params, function(err, data) {
if (err) {
console.log("Error uploading data: ", err);
} else {
console.log("Successfully uploaded data to myBucket" + JSON.stringify(data));
}
});
router.post("/upload-image", (req, res) => {
let imageFileName;
const busboy = new BusBoy({ headers: req.headers });
busboy.on("file", (fieldName, file, fileName, encoding, mimetype) => {
if (mimetype !== "image/jpeg" && mimetype !== "image/png") {
return res.status(400).json({ error: "Bad format in the image" });
}
const filePath = path.join(os.tmpdir(), fileName);
const params = {
Bucket: "your bucket name",
Key: fileName,
Body: "",
ACL: "public-read",
ContentType: mimetype,
};
file.on("data", function (data) {
params.ContentLength = data.length;
params.Body = data;
});
// needed to call upload function after a while, because params was not updating immediately
setTimeout(() => {
s3.upload(params, (error, data) => {
if (error) {
return res.status(500).send(error);
}
return res.status(200).json({ imageURL: data.Location });
});
}, 1000);
file.pipe(fs.createWriteStream(filePath));
});
busboy.end(req.rawBody);
});