Storing user submitted images

92 views Asked by At

I'm building a node application in which users can submit images to customize their profile. I'm wondering what the best way would be to store these images? Is something like Amazon S3 the way to go? What about CloudFront, can this accept user submitted images? Sorry if this question is a little broad but I'm unsure where to start. Thanks!

1

There are 1 answers

0
siavolt On BEST ANSWER

i think you can use knox for you task, for example.

First you need create "class" with s3 api. Do this with knox:

var s3 = function(args){
    this.client = knox.createClient({
        key: '<api-key-here>'
        secret: '<secret-here>'
        bucket: 'learnboost'
    });
}

s3.prototype = {
    getImage: function(userId, imageId){          
        var d = Q.defer();
        var image = "";

        client.get(path.join("images", userId, imageId)).on('response', function(res){
            res.on('data', function(chunk){
                image += chunk; 
            });

            res.on('end', function(){
                d.resolve(image);
            });
        }).end();

        return d.promise;
    },

    saveImage: function(userId, imageId, imageBuffer){
        var d = Q.defer();
        this.client.putBuffer(imageBuffer, path.join("images", userId, imageId), function(err, res){
            if(err){ 
                d.reject(err);
            }

            return d.resolve();
        });

        return d.promise;
    }
};

You can get image from s3 without knox, but if you need this in the project you can use. For save image to s3 use saveImage method in you multipart routes.

Good luck