我正在尝试构建一个应用程序来操作图像并将它们上传到 s3。
该代码在 localhost 上完美运行。但是当我将相同的代码部署到heroku时,s3 上上传的图像为 0 字节空:
这是我的代码:
const fs = require('fs');
const uuid = require('node-uuid');
const Promise = require('bluebird');
const gm = require('gm').subClass({imageMagick: true});
const mime = require('mime');
const Config = require('../../../config/settings');
const AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: Config.get('/s3/accessKeyId'),
secretAccessKey: Config.get('/s3/secretAccessKey'),
region: Config.get('/s3/region')
});
const s3 = new AWS.S3();
const createImage = function (url, width, height, fileName, fileExt) {
return new Promise(function (resolve, reject) {
if (typeof fileName === "undefined") {
fileName = uuid.v1();
}
if (typeof fileExt === "undefined") {
fileExt = "jpg";
}
const fullFileName = `${fileName}.${fileExt}`;
gm(url)
.resize(width, height, '!')
.autoOrient()
.stream(function (err, stdout, stderr) {
var data = {
Bucket: Config.get('/s3/bucketName'),
Key: fullFileName,
Body: stdout,
ContentType: mime.lookup(fullFileName)
};
s3.upload(data, function (err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
});
};
module.exports = createImage;
谢谢您的帮助!