diff --git a/lib/dynamo-restore.js b/lib/dynamo-restore.js index b341fb8..a1119d1 100755 --- a/lib/dynamo-restore.js +++ b/lib/dynamo-restore.js @@ -24,12 +24,16 @@ function DynamoRestore(options) { options.awsKey = options.awsKey || process.env.AWS_ACCESS_KEY_ID; options.awsSecret = options.awsSecret || process.env.AWS_SECRET_ACCESS_KEY; options.awsRegion = options.awsRegion || process.env.AWS_DEFAULT_REGION || 'ap-southeast-2'; - - AWS.config.update({ - accessKeyId: options.awsKey, - secretAccessKey: options.awsSecret, + var awsConfigData = { region: options.awsRegion - }); + } + + if ( options.awsSecret && options.awsRegion ) { + awsConfigData.accessKeyId = options.awsKey; + awsConfigData.secretAccessKey = options.awsSecret; + } + + AWS.config.update(awsConfigData); this.options = options; this.dynamodb = new AWS.DynamoDB(); @@ -107,7 +111,8 @@ DynamoRestore.prototype._checkTableExists = function(error, data) { if (this.options.overwrite) { this.emit('warning', util.format('WARN: table [%s] will be overwritten.', this.options.table)); setTimeout(dynamodb.describeTable.bind(dynamodb, { TableName: this.options.table }, this._checkTableReady.bind(this)), 1000); - } else { + } + else { this.emit('error', 'Fatal Error. The destination table already exists! Exiting process..'); } return; @@ -145,10 +150,15 @@ DynamoRestore.prototype._startDownload = function() { .on('line', this._processLine.bind(this)) .on('close', (function() { this.emit('finish-download'); - this.batches.push({ - items: this.requestItems.splice(0, DYNAMO_CHUNK_SIZE), - attempts: 0 - }); + // if (!this.batches) { + // this.batches = [] + // } + if (this.requestItems && this.requestItems.length) { + this.batches.push({ + items: this.requestItems.splice(0, DYNAMO_CHUNK_SIZE), + attempts: 0 + }); + } this._finishBatches(); }).bind(this)); this.readline.meta = meta; @@ -281,7 +291,7 @@ DynamoRestore.prototype._checkTableReady = function(error, data) { // All ready, now we can start inserting records this.tableready = true; this.readline.resume(); - while (this.batches.length) { this._sendBatch(); } + while (this.batches && this.batches.length) { this._sendBatch(); } } else { setTimeout(dynamodb.describeTable.bind(dynamodb, { TableName: data.Table.TableName }, this._checkTableReady.bind(this)), 1000); } @@ -344,8 +354,8 @@ DynamoRestore.prototype._sendBatch = function() { }; DynamoRestore.prototype._finishBatches = function() { - if (!this.batches.length) { - if (!this.requests.length) { + if (!this.batches || !this.batches.length) { + if (!this.requests || !this.requests.length) { // Finish only if there is nothing left to wait for. this.emit('finish'); return; @@ -360,4 +370,4 @@ DynamoRestore.prototype._finishBatches = function() { setTimeout(this._finishBatches.bind(this), 200); }; -module.exports = DynamoRestore; \ No newline at end of file +module.exports = DynamoRestore;