我有一个简单的express应用程序,它接收POST请求的主体(以XML形式)并将其推送到Amazon S3存储桶上的文件中。
我的POST路由如下:
var port = process.env.PORT || 3000
, express = require("express")
, app = express()
, aws = require('aws-sdk')
, xmlParser = require("express-xml-bodyparser");
//App Config
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(xmlParser());
//Set up AWS S3 instance
aws.config.update({
accessKeyId: "id"
, secretAccessKey: "key"
});
var s3 = new aws.S3();
//Post route
app.post("/", function(req, res) {
var params = {
Bucket: 'bucket'
, Key: req.get("Filename")
, Body: req.body
, ACL: "public-read"
};
s3.putObject(params, function(perr, pres) {
if (perr) {
res.writeHead(500, 'Unable to write to S3 bucket', {
'Content-Type': 'text/plain'
});
res.end();
console.log("Error uploading data: ", perr);
} else {
console.log("Successfully uploaded data to bucket/" + req.get("Filename"));
}
});
res.writeHead(200, 'OK', {
'Content-Type': 'text/plain'
});
res.end();
});
但是,由于params对象的Body变量需要为字符串,但由于作为对象而出现,因此出现错误。
如何将XML主体转换为可以保存到S3实例的XML字符串?