javascript - 使用 Gulp 上传资源到 s3

标签 javascript node.js amazon-s3 gulp aws-sdk

我目前正在尝试使用 aws-sdk 和 gulp 将网站的 Assets 上传到 amazon s3,但现在我只是使用以下代码实现了上传单个文件:

gulp.task('publish', function() {
    var AWS = require('aws-sdk'),
        fs = require('fs');

    AWS.config.accessKeyId = 'access_id';
    AWS.config.secretAccessKey = 'secret_key';
    AWS.config.region = 'eu-central-1';
    var fileStream = fs.createReadStream('folder/filename');
    fileStream.on('error', function (err) {
            if (err) { throw err; }
        });
    fileStream.on('open', function () {
    var s3 = new AWS.S3();
            s3.putObject({
                    Bucket: 'bucket_name',
                        Key: 'assets/filename',
                        Body: fileStream,
                        ACL:'public-read'
                        }, function (err) {
        if (err) { throw err; }
        else { console.log("Upload successfull"); }
                });
        });
});

因为我既不是 node.js 也不是 JS 开发者,所以我不知道如何将我的所有 Assets 上传到 S3 的文件夹 Assets 中。

理想情况下,应用我用来上传一个文件的操作,但对于每个文件来说会很整洁。这怎么可能?

最佳答案

找到了我的问题的解决方案。使用此代码,我最终成功地将我所有的 Assets 上传到我的存储桶中,并使用了正确的 ACL。希望这可以帮助人们不要像我一样花那么多时间在这样一个愚蠢的问题上。

/*
* Dependencies
*/

var gulp = require('gulp');
var AWS  = require('aws-sdk');
var fs   = require('fs');
var walk = require('walk');

/*
 * Declaration of global variables
 */


var isPaused = false;

/*
 * Bucket access informations
 */

AWS.config.accessKeyId = 'access_keyid'
AWS.config.secretAccessKey = 'secret_access_key'
AWS.config.region = 'region';

/*
 * Publishing function: uses a stream to push the files on the AWS Bucket
 */

function publishit(filename) {
var file = filename.substring('./'.length);
var key = file.substring('src/'.length);
var fileStream = fs.createReadStream(file);
isPaused = true;

// Check if there is an error on the file
fileStream.on('error', function (err) {
        if (err) { throw err; }
    });

// Action to do on opening of the file
fileStream.on('open', function () {
        var s3 = new AWS.S3();

        // Uploading the stream to the bucket
        s3.putObject({
                Bucket: 'bucket_name',
                    Key: key,
                    Body: fileStream,
                    ACL:'public-read'
                    }, function (err) {
                // Show the error if there is any
                if (err) { throw err; }

                // If everything went successfully, print which file is being uploaded
                else { console.log("Uploading asset "+ file); }

                // Closing the stream to avoid leaks and socket timeouts
                fileStream.close();

                // Changing the status of 'isPaused' to false to continue uploading the other assets
                isPaused = false;
            });
    });
}

gulp.task('assets', function() {

    var files   = [];

    // Walker options (first arg is the folder you want to upload)
    var walker  = walk.walk('./assets', { followLinks: false });

    walker.on('file', function(root, stat, next) {
            // Add this file to the list of files
            files.push(root + '/' + stat.name);
            next();
        });

    // Action after every file has been added to 'files'
    walker.on('end', function() {
            for (var filename in files){
                // Publish every file added to 'files'
                publishit(files[filename]);
                // Wait for one push on the server to be done before calling the next one
                function waitForIt(){
                    if (isPaused) {
                        setTimeout(function(){waitForIt()},100);
                    }
                };

            };
        });
});

关于javascript - 使用 Gulp 上传资源到 s3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33010607/

相关文章:

Android - 如何从 s3 下载图像?

amazon-s3 - Amazon S3 客户端无法下载带有空格或散列的文件?

javascript - 更改 Bootstrap 菜单断点

javascript - 处理ajax响应异常

node.js - 如何使用 JWT 在 Express 中为单个路由(无代码重复)验证多种类型用户的身份验证 token ?

python - 想要创建文件时 Lambda 和 S3 权限被拒绝

javascript - Firebase 按日期排序从今天起的值不起作用

javascript - 正在处理 JS 作业(函数、数组等)——有几个问题

javascript - SignalR 核心 - 尝试将 clientId 传递给 Hub,无法弄清楚如何接收

node.js - 如何在 Node.js 中请求 GSAP