5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[AWS] nodejs で s3 sync

Last updated at Posted at 2016-07-12

Lambda で S3 sync したい時とかってありますよね、無いですか?私はあったんです。

まずは npm から node-s3-client をインストール

$ npm install s3 --save

そんで、こんな感じで書きます。
Lambda や EC2 に適切な IAM ロールが割り当てられている場合は s3.createClient() に渡す s3Options.accessKeyId, s3Options.secretAccessKey は省略可能。

s3sync.js
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");
});

すっきり

参考URL: javascript - Upload entire directory tree to S3 using AWS sdk in node js - Stack Overflow

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?