簡體   English   中英

使用node.js轉到s3存儲

[英]Moving to s3 storage with node.js

我正准備移動一個node.js應用程序,我一直在努力進入生產環境(我正在使用Heroku)。 用戶通過URL將圖像添加到網站。 現在他們只是保存在服務器上 - 我想將存儲移到s3但是我遇到了一些困難。

我的想法是首先將圖像保存到磁盤然后上傳,但是當文件寫入磁盤時,我很難找到通知的方法。 它似乎不使用典型的節點回調樣式。

這是現在的代碼。 我正在使用請求節點模塊 ,這可能使事情復雜化而不是簡化它們:

    requestModule(request.payload.url).pipe(fs.createWriteStream("./public/img/submittedContent/" + name));

                // what do I do here?

                fs.readFile("./public/img/submittedContent/" + name, function(err, data){
                if (err) { console.warn(err); }
                else {
                    s3.putObject({
                        Bucket: "submitted_images",
                        Key: name,
                        Body: data
                     }).done(function(s3response){
                        console.log("success!");
                        reply({message:'success'});
                    }).fail(function(s3response){
                        console.log("failure");
                        reply({message:'failure'});
                    });
                }
            });

任何意見,將不勝感激。 謝謝!

嘗試在可寫流上偵聽finish事件:

requestModule(request.payload.url).pipe(fs.createWriteStream("./public/img/submittedContent/" + name)).on('finish', function(){
    // do stuff with saved file
});

除非您正在修改圖像,否則不應上傳到Heroku - 而應直接上傳到S3。

請參閱Node.js中的直接到S3文件上載

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM