使用Connect Multiparty,Jimp和Amazon s3fs节点模块将resize的照片上传到Amazon s3

我目前有以下代码设置从客户端发送到一个快速服务器的文件。

const data = new FormData() data.append('programPhoto', imageFromFileInput, fileName) $.ajax({ url: requestUrl, data: data, cache: false, contentType: false, processData: false, type: 'POST' }) 

在服务器上,我使用Connect-Multiparty将req对象上的这个图像文件作为req.files [0]

  const multiParty = require('connect-multiparty'); const multiPartyMiddleware = multiParty(); app.use(multiPartyMiddleware); 

我有一个帮助function,该文件并将其上传到亚马逊S3成功和伟大的工作。 它看起来像这样:

  const constants = require('../config/constants'); const keys = require('../config/config.js') const fs = require('fs'); const S3FS = require('s3fs'); const Jimp = require('jimp') const s3fsImp1 = new S3FS(constants.BUCKET_NAME, { accessKeyId: keys.AWS_ACCESS_KEY, secretAccessKey: keys.AWS_SECRET_KEY }); const uploadToAmazonS3 = function(file) { const stream = fs.createReadStream(file.path); const programName = file.originalFilename.split('_')[0]; // setting a fileName to make a useful URL const fileName = programName; return s3fsImp1.writeFile(fileName, stream) .then(function() { fs.unlink(file.path, function(err) { if (err) { console.error('error uploading to s3 ', err); } }) const encodedFilename = fileName.replace(/ /gi, '+'); const url = 'https://s3.amazonaws.com/' + constants.BUCKET_NAME + '/' + encodedFilename; console.log('uploaded to s3. URL: ', url); // return image URL and photoId from DB so we can find the row by ID and update the URL return { url: url, Id: photoId }; }) .catch(function() { console.error('error uploading to S3') }) } 

此代码运行良好,照片正在上传,URL正确匹配,但是现在我需要在上传到S3之前调整图像大小或裁剪图像。 我正在尝试使用Jimp npm模块

从文档来看,Jimp的基本用法如下:

 Jimp.read("./path/to/image.jpg").then(function (image) { // do something with the image, example: return image.resize(250, 250) .quality(60) }).catch(function (err) { // handle an exception }); 

Jimp也可以读取缓冲区:

 Jimp.read(lenna.buffer, function (err, image) { // do stuff with the image (if no exception) }); 

然而,无论我如何调整代码以将其加载到导致上传到S3的事件链中,我都无法让Jimp模块读取通过connect-multiparty发送的文件。 文件对象如下所示:

 { fieldName: 'programPhoto', originalFilename: 'Screen Shot 2016-04-10 at 12.20.50 PM.png', path: '/var/folders/rl/b20b02js419dvh736nkydw6w0000gn/T/ROo9HeqPYBggfTZ1tUK7Zdkx.png', headers: { 'content-disposition': 'form-data; name="programPhoto"; filename="Screen Shot 2016-04-10 at 12.20.50 PM.png"', 'content-type': 'image/png' }, size: 184932, name: 'Screen Shot 2016-04-10 at 12.20.50 PM.png', type: 'image/png' } 

如果我试图做Jimp.read(file.path).then(function(image){// do stuff})。 )

我得到以下错误logging:

 error reading with Jimp Jimp { _originalMime: 'image/png', bitmap: { data: <Buffer dc dd dd ff dc dd dd ff dc dd dd ff dc dd dd ff dc dd dd ff dc dd dd ff dc dd dd ff dc dd dd ff dc dd dd ff dc dd dd ff dc dd dd ff dc dd dd ff dc dd ... >, width: 568, height: 414 } } 

我真的很想用Jimp在请求文件上使用这个对象,反正有没有改变我发送文件到服务器的方式?