Node.js NodeJS-在发送到AWS S3之前处理图像
好吧,这让我今天有些困惑,我相信有一个简单的解决办法。问题在于发送到S3之前的图像处理,这是我在保存和使用AWS之前处理图像的方式:Node.js NodeJS-在发送到AWS S3之前处理图像,node.js,image-processing,amazon-web-services,amazon-s3,Node.js,Image Processing,Amazon Web Services,Amazon S3,好吧,这让我今天有些困惑,我相信有一个简单的解决办法。问题在于发送到S3之前的图像处理,这是我在保存和使用AWS之前处理图像的方式: // Form var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { res.writeHead(200, {'content-type': 'text/plain'}); }); //
// Form
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
});
// Form On
form.on('end', function(fields, files) {
/* Temporary location of our uploaded file */
var temp_path = this.openedFiles[0].path;
/* The file name of the uploaded file */
var file_name = this.openedFiles[0].name;
/* Location where we want to copy the uploaded file */
var new_location = 'public/assets/uploads/blog/';
// Rename Image
var t = path.extname(file_name);
var n = Math.floor(new Date() / 1000);
// Copy Image
fse.copy(temp_path, new_location+n+t, function(err) {
if (err) {
console.error(err);
} else {
//
// Resize and Blur
//
require('lwip').open(new_location+n+t, function(err, image) {
image.batch()
//.scale(0.75) // scale to 75%
//.rotate(45, 'white') // rotate 45degs clockwise (white fill)
//.crop(200, 200) // crop a 200X200 square from center
.blur(7) // Gaussian blur with SD=5
.writeFile('public/assets/uploads/blog/blur/'+n+t, function(err){
// check err...
if(err) {
console.log(err);
}
// done.
console.log('Success');
// Send Data
res.write(n+t);
res.end();
});
});
}
});
});
很简单的东西,对吧。我所做的一切就是使用Knowledge处理传入的表单,然后使用“lwip”调整大小(如果需要)并模糊图像副本(放置在新目录中)
那么,在将数据发送到AWS S3之前,我如何使用“lwip”来调整大小和模糊呢 好的,我现在回答我自己的问题,这很酷。这就是我在本地和Heroku上都成功的想法。我使用了s3模块上传器
var client = s3.createClient({
maxAsyncS3: 20, // this is the default
s3RetryCount: 0, // this is the default
s3RetryDelay: 1000, // this is the default
multipartUploadThreshold: 20971520,
multipartUploadSize: 15728640,
s3Options: {
accessKeyId: "key",
secretAccessKey: "secret",
},
});
exports.s3 = function(req, res, next) {
// Formidable
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
});
form.on('end', function(fields, files) {
/* Temporary location of our uploaded file */
var temp_path = this.openedFiles[0].path;
/* The file name of the uploaded file */
var file_name = this.openedFiles[0].name;
/* Location where we want to copy the uploaded file */
var new_location = 'public/assets/uploads/s3/';
// Rename Image
var e = path.extname(file_name);
var n = Math.floor(new Date() / 1000);
// Copy Image
fse.copy(temp_path, new_location+n+e, function(err) {
// AWS Params
var params = {
localFile: new_location+n+e,
s3Params: {
Bucket: "hirelee-uploads",
Key: "blog/"+n+e,
},
};
// AWS Upload
var uploader = client.uploadFile(params);
uploader.on('error', function(err) {
console.error("unable to upload:", err.stack);
});
uploader.on('end', function() {
// Blur Copied Image
require('lwip').open(params.localFile, function(err, image) {
image.batch()
.blur(7)
.writeFile("public/assets/uploads/s3/blur-"+n+e, function(err){
// check err...
if(err) {
console.log(err);
} else {
// AWS Upload Blur
var params = {
localFile: "public/assets/uploads/s3/blur-"+n+e,
s3Params: {
Bucket: "hirelee-uploads",
Key: "blog/blur/"+n+e,
},
};
var uploader = client.uploadFile(params);
uploader.on('error', function(err) {
console.error("unable to upload:", err.stack);
});
// Finished AWS upload
uploader.on('end', function() {
console.log("done uploading");
// Delete Copied Images on Disk
fs.unlinkSync("public/assets/uploads/s3/"+n+e)
fs.unlinkSync("public/assets/uploads/s3/blur-"+n+e)
res.end();
});
}
});
});
});
});
如果有人能给我一个呼喊,如果我错过了任何将是伟大的或可以使上述更有效的