节点gm - s3在heroku上传(0字节)

时间:2016-09-08 19:08:45

标签: node.js heroku amazon-s3 upload gm

我正在尝试构建一个应用程序来处理图像并将其上传到s3上。

代码在localhost上完美地 。但是当我将相同的代码部署到 heroku 时,s3上的上传图像为0字节为空:

this screenshot shows one file uploaded via localhost and one via heroku (0 byte)

这是我的代码:

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;

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

问题是由heroku上的imagemagick政策更新造成的:https://devcenter.heroku.com/changelog-items/891

我写了一个更具体的新问题:imagemagick - change policy.xml on heroku

答案 1 :(得分:0)

我对俱乐部来说有点晚了,但我可能有一个过去对我有用的解决方法,也许它会为那些遇到这个问题的人工作。您可以更改传递给http。

的url参数
const newURL = url.replace("https", "http");

gm(newURL)
  .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);
          }
      });
  });