0

I'm trying to build an app to manipulate images and upload them on s3.

The code works perfectly on localhost. But when I deploy the same code to heroku, the uploaded image on s3 is 0 byte empty:

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

here is my code:

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;

Thank you for your help!

JohnnyTheTank
  • 668
  • 5
  • 14

2 Answers2

0

The problem is caused by this imagemagick policy update on heroku: https://devcenter.heroku.com/changelog-items/891

I wrote a new, more specific question: imagemagick - change policy.xml on heroku

Community
  • 1
  • 1
JohnnyTheTank
  • 668
  • 5
  • 14
0

I'm a little late to the club but I might have a workaround that has worked for me in the past and maybe it'll work for others who come by this issue. You can change the url parameter that is passed to http.

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);
          }
      });
  });
Dylan
  • 63
  • 2
  • 9