gpt4 book ai didi

amazon-s3 - 无法使用无服务器框架将图像上传到 s3,但它可以离线工作(缓冲区问题)

转载 作者:行者123 更新时间:2023-12-04 10:32:21 27 4
gpt4 key购买 nike

我正在尝试部署一个 lambda 函数,允许我将图片上传到 S3。
lambda 在离线状态下运行良好,但是当我将其部署到 AWS 时,该功能不起作用。

我遇到的第一个错误是这个:

ERROR   (node:7) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

所以,我遵循了使用 Buffer.from() 方法的建议。但它也不起作用。 lambda 一直运行到超时。

有人可以告诉我我错在哪里或建议我另一种解决方案吗?

在我的 lambda 函数下面:
const AWS = require("aws-sdk");
const Busboy = require("busboy");
const uuidv4 = require("uuid/v4");
require("dotenv").config();

AWS.config.update({
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY,
subregion: process.env.SUB_REGION
});

const s3 = new AWS.S3();

const getContentType = event => {
// see the second block of codes
};

const parser = event => {
// see the third block of codes
};


module.exports.main = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const uuid = uuidv4();

const uploadFile = async (image, uuid) =>
new Promise(() => {
// const bitmap = new Buffer(image, "base64"); // <====== deprecated
const bitmap = Buffer.from(image, "base64"); // <======== problem here
const params = {
Bucket: "my_bucket",
Key: `${uuid}.jpeg`,
ACL: "public-read",
Body: bitmap,
ContentType: "image/jpeg"
};
s3.putObject(params, function(err, data) {
if (err) {
return callback(null, "ERROR");
}
return callback(null, "SUCCESS");
});
});

parser(event).then(() => {
uploadFile(event.body.file, uuid);
});
};

getContentType() :
const getContentType = event => {
const contentType = event.headers["content-type"];
if (!contentType) {
return event.headers["Content-Type"];
}
return contentType;
};

解析器()
const parser = event =>
new Promise((resolve, reject) => {
const busboy = new Busboy({
headers: {
"content-type": getContentType(event)
}
});

const result = {};

busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
file.on("data", data => {
result.file = data;
});

file.on("end", () => {
result.filename = filename;
result.contentType = mimetype;
});
});

busboy.on("field", (fieldname, value) => {
result[fieldname] = value;
});

busboy.on("error", error => reject(error));
busboy.on("finish", () => {
event.body = result;
resolve(event);
});

busboy.write(event.body, event.isBase64Encoded ? "base64" : "binary");
busboy.end();
});

最佳答案

new Buffer(number)            // Old
Buffer.alloc(number) // New

new Buffer(string) // Old
Buffer.from(string) // New

new Buffer(string, encoding) // Old
Buffer.from(string, encoding) // New

new Buffer(...arguments) // Old
Buffer.from(...arguments) // New

关于amazon-s3 - 无法使用无服务器框架将图像上传到 s3,但它可以离线工作(缓冲区问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60361929/

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