0

I am writing an Express app that takes in a base64 encoded string that represents an image. Right now, i'm not really sure how I can take that string and upload the image to AWS S3, so i'm reading in the encoded image string data, decoding it, writing a file using fs, and then trying to upload. I have this working for an endpoint that just takes in a raw file, and all of its content is correctly uploaded to AWS s3.

Now when I try to do what I described above, i'm able to upload to S3, but the file has 0kb and is empty, and i'm not sure why. I tested just taking the stringData and writing a file to a test file, and it works. However, when I try uploading to s3, the file shows but it's empty. Here is my code:

router.post('/images/tags/nutritionalInformation/image/base64encoded', function (req, res) {
    console.log(req.body.imageString);
    var base64Stream = req.body.imageString;
    var imgDecodedBuffer = decodeBase64Image(base64Stream);
    console.log(imgDecodedBuffer);
    // write to image file
    var prefix = guid().toString() + ".jpg";
    var filePath = './uploads/' + prefix;
    console.log(filePath);


    fs.writeFile(filePath, imgDecodedBuffer.data, function(err) {
        console.log(err);
    });

    var stream = fs.createReadStream(filePath);


    console.log(stream);

    return s3fsImpl.writeFile(prefix, stream).then(function () {
        fs.unlink(filePath, function (err) {
            if (err) {
                console.error(err);
            }
        });
    });
})

Here are the relevant import statements:

var fs = require('fs');
var s3fs = require('s3fs');
var multiparty = require('connect-multiparty'),
    multipartyMidleware = multiparty();

var s3fsImpl = new s3fs('blahblah', {
    accessKeyId: 'ACCESS_KEY_ID',
    secretAccessKey: 'SECRET'
});

Any help would be greatly appreciated!

1 Answer 1

1

If you simply just pass in the buffer, which I presume is in your imgDecodedBuffer.data value, it should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.