Node.js 是否使用NodeJS检索远程文件以进行存储?

Node.js 是否使用NodeJS检索远程文件以进行存储?,node.js,mongodb,amazon-s3,mongoose,passport.js,Node.js,Mongodb,Amazon S3,Mongoose,Passport.js,我有nodejsrestify设置,我使用mongoose附件将图像附加到我的用户模型,并将图像存储在S3存储桶中 我还允许用户使用Passport.JS使用Facebook、Google等注册 问题是mongoose attachments在调用.attach()函数时需要一个本地文件引用,PassportJS提供了一个远程URL,所以我需要下载图像,然后从tmp附加它 我应该如何处理NodeJS?是否有一个好的模块可供我使用?您有没有看过您有没有看过我通过该模块找到了一个有效的解决方案。它满

我有nodejsrestify设置,我使用mongoose附件将图像附加到我的用户模型,并将图像存储在S3存储桶中

我还允许用户使用Passport.JS使用Facebook、Google等注册

问题是mongoose attachments在调用.attach()函数时需要一个本地文件引用,PassportJS提供了一个远程URL,所以我需要下载图像,然后从tmp附加它


我应该如何处理NodeJS?是否有一个好的模块可供我使用?

您有没有看过

您有没有看过

我通过该模块找到了一个有效的解决方案。它满足了我的需要,似乎是任何web应用程序的全面工具。这就是有效的代码:

var userImageProp = {};
if ((profile.picture) && (profile.picture.data.is_silhouette == false)) {

    var pictureURL = 'https://graph.facebook.com/'+ profile.id +'/picture?type=large';    

    // Determine file name.
    var filename = profile.picture.data.url.replace(/^.*[\\\/]/, '');

    // Precreate stream and define callback.
    var picStream = fs.createWriteStream('/tmp/'+filename);
    picStream.on('close', function() {
        console.log('Downloaded '+filename);
        userImageProp.path = '/tmp/'+filename;
        finishSave(user, userImageProp);
    });

    // Get and save file.
    request(pictureURL).pipe(picStream);

} else {
    userImageProp.path = config.root + '/defaults/img/faceless_'+user.gender.toLowerCase()+'.png';
    finishSave(user, userImageProp);
}

function finishSave(user, userImageProp) {
    user.attach('userImage', userImageProp, function(err) {
        console.dir(err);
        if (err) { return done(new restify.InternalError(err)); }
        user.save(function (err, user) {
            if (err) { return done(new restify.InternalError(err)); }
            // Saved successfully. Return user for login, and forward client to complete user creation.
            return done(null, user, '/#/sign-up/facebook/save');
        });
    });
}
感谢这些线程帮助我想出了这个解决方案:


我通过该模块找到了解决此问题的有效方法。它满足了我的需要,似乎是任何web应用程序的全面工具。这就是有效的代码:

var userImageProp = {};
if ((profile.picture) && (profile.picture.data.is_silhouette == false)) {

    var pictureURL = 'https://graph.facebook.com/'+ profile.id +'/picture?type=large';    

    // Determine file name.
    var filename = profile.picture.data.url.replace(/^.*[\\\/]/, '');

    // Precreate stream and define callback.
    var picStream = fs.createWriteStream('/tmp/'+filename);
    picStream.on('close', function() {
        console.log('Downloaded '+filename);
        userImageProp.path = '/tmp/'+filename;
        finishSave(user, userImageProp);
    });

    // Get and save file.
    request(pictureURL).pipe(picStream);

} else {
    userImageProp.path = config.root + '/defaults/img/faceless_'+user.gender.toLowerCase()+'.png';
    finishSave(user, userImageProp);
}

function finishSave(user, userImageProp) {
    user.attach('userImage', userImageProp, function(err) {
        console.dir(err);
        if (err) { return done(new restify.InternalError(err)); }
        user.save(function (err, user) {
            if (err) { return done(new restify.InternalError(err)); }
            // Saved successfully. Return user for login, and forward client to complete user creation.
            return done(null, user, '/#/sign-up/facebook/save');
        });
    });
}
感谢这些线程帮助我想出了这个解决方案:


这就是我用来将文件附加到我的模型并上载到S3的方法-但是它需要一个本地文件。这就是我用来将文件附加到我的模型并上载到S3的方法-但是它需要一个本地文件。