[亚马逊云服务] 使用Node.js进行S3同步

您是否有时需要在Lambda上进行S3同步呢?我遇到过这种情况。

首先,从 npm 安装 node-s3-client。

$ npm install s3 --save

所以,以这样的形式进行编写。
如果Lambda或EC2已经分配了合适的IAM角色,则可以省略传递给s3.createClient()的s3Options.accessKeyId和s3Options.secretAccessKey。

var s3 = require('s3');
var client = s3.createClient({
    maxAsyncS3: 20,     // this is the default
    s3RetryCount: 3,    // this is the default
    s3RetryDelay: 1000, // this is the default
    multipartUploadThreshold: 20971520, // this is the default (20 MB)
    multipartUploadSize: 15728640, // this is the default (15 MB)
    s3Options: {
        // accessKeyId: "your s3 key",
        // secretAccessKey: "your s3 secret",
        region: 'us-east-1',
        // endpoint: 's3.yourdomain.com',
        // sslEnabled: false
        // any other options are passed to new AWS.S3()
        // See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property
    },
});

var params = {
    localDir: '/path/to/localdir',
    deleteRemoved: false, // default false, whether to remove s3 objects
                          // that have no corresponding local file.
    s3Params: {
        Bucket: 'your S3 bucket name',
        // Prefix: "some/remote/dir/",
        // other options supported by putObject, except Body and ContentLength.
        // See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
    },
};

var uploader = client.uploadDir(params);
uploader.on('error', function(err) {
    console.error("unable to sync:", err.stack);
});
uploader.on('progress', function() {
    console.log("progress", uploader.progressAmount, uploader.progressTotal);
});
uploader.on('end', function() {
    console.log("done uploading");
});

爽快

参考链接:javascript – 使用AWS Node.js SDK上传整个目录树到S3 – Stack Overflow

广告
将在 10 秒后关闭
bannerAds