存储用户提交的图像

时间:2015-06-15 14:09:07

标签: node.js file-upload amazon-s3 amazon-cloudfront

我正在构建一个节点应用程序,用户可以在其中提交图像以自定义其配置文件。我想知道存储这些图像的最佳方法是什么?是不是像亚马逊S3这样的方式?那么CloudFront,这可以接受用户提交的图像吗?对不起,如果这个问题有点宽泛,但我不确定从哪里开始。谢谢!

1 个答案:

答案 0 :(得分:0)

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