2015-08-24 11:13:21 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2016-04-04 11:43:56 -07:00
|
|
|
getRestoreUrl: getRestoreUrl,
|
2015-08-26 16:14:51 -07:00
|
|
|
|
2015-11-08 10:54:47 -08:00
|
|
|
copyObject: copyObject,
|
2016-04-10 10:55:59 -07:00
|
|
|
getAllPaged: getAllPaged,
|
|
|
|
|
|
|
|
|
|
getBackupCredentials: getBackupCredentials
|
2015-08-24 11:13:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2015-11-08 10:54:47 -08:00
|
|
|
AWS = require('aws-sdk');
|
2015-08-24 11:13:21 -07:00
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
function getBackupCredentials(apiConfig, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2015-08-24 11:13:21 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
assert(apiConfig.accessKeyId && apiConfig.secretAccessKey);
|
2015-11-06 18:22:29 -08:00
|
|
|
|
|
|
|
|
var credentials = {
|
2016-03-31 09:48:01 -07:00
|
|
|
accessKeyId: apiConfig.accessKeyId,
|
|
|
|
|
secretAccessKey: apiConfig.secretAccessKey,
|
2016-03-31 09:48:38 -07:00
|
|
|
region: apiConfig.region || 'us-east-1'
|
2015-11-06 18:22:29 -08:00
|
|
|
};
|
2015-09-09 11:43:50 -07:00
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
if (apiConfig.endpoint) credentials.endpoint = new AWS.Endpoint(apiConfig.endpoint);
|
2015-09-09 11:43:50 -07:00
|
|
|
|
2015-11-06 18:22:29 -08:00
|
|
|
callback(null, credentials);
|
2015-08-24 11:13:21 -07:00
|
|
|
}
|
2015-08-25 10:01:04 -07:00
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
function getAllPaged(apiConfig, page, perPage, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2015-11-08 10:54:47 -08:00
|
|
|
assert.strictEqual(typeof page, 'number');
|
|
|
|
|
assert.strictEqual(typeof perPage, 'number');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
getBackupCredentials(apiConfig, function (error, credentials) {
|
2015-12-30 18:21:38 +01:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var s3 = new AWS.S3(credentials);
|
|
|
|
|
|
|
|
|
|
var params = {
|
2016-03-31 09:48:01 -07:00
|
|
|
Bucket: apiConfig.bucket,
|
2015-12-30 18:21:38 +01:00
|
|
|
EncodingType: 'url',
|
2016-03-31 09:48:01 -07:00
|
|
|
Prefix: apiConfig.prefix + '/backup_'
|
2015-12-30 18:21:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
s3.listObjects(params, function (error, data) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var results = data.Contents.map(function (backup) {
|
|
|
|
|
return {
|
|
|
|
|
creationTime: backup.LastModified,
|
2016-03-31 09:48:01 -07:00
|
|
|
restoreKey: backup.Key.slice(apiConfig.prefix.length + 1),
|
2016-01-19 13:20:05 +01:00
|
|
|
dependsOn: [] // FIXME empty dependsOn is wrong and version property is missing!!
|
2015-12-30 18:21:38 +01:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2015-12-30 18:31:44 +01:00
|
|
|
results.sort(function (a, b) { return a.creationTime < b.creationTime; });
|
|
|
|
|
|
2015-12-30 18:21:38 +01:00
|
|
|
return callback(null, results);
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-11-08 10:54:47 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-04 11:44:24 -07:00
|
|
|
function getRestoreUrl(apiConfig, filename, callback) {
|
2016-03-31 09:48:01 -07:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2016-03-31 09:53:56 -07:00
|
|
|
assert.strictEqual(typeof filename, 'string');
|
2015-08-25 10:01:04 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
getBackupCredentials(apiConfig, function (error, credentials) {
|
2015-08-25 10:01:04 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var s3 = new AWS.S3(credentials);
|
|
|
|
|
|
|
|
|
|
var params = {
|
2016-04-04 11:44:24 -07:00
|
|
|
Bucket: apiConfig.bucket,
|
|
|
|
|
Key: apiConfig.prefix + '/' + filename,
|
2015-08-25 10:01:04 -07:00
|
|
|
Expires: 60 * 30 /* 30 minutes */
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-27 09:26:19 -07:00
|
|
|
var url = s3.getSignedUrl('getObject', params);
|
|
|
|
|
|
2016-04-03 23:23:23 -07:00
|
|
|
callback(null, { url: url });
|
2015-08-25 10:01:04 -07:00
|
|
|
});
|
|
|
|
|
}
|
2015-08-26 16:14:51 -07:00
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
function copyObject(apiConfig, from, to, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2015-09-21 14:02:00 -07:00
|
|
|
assert.strictEqual(typeof from, 'string');
|
|
|
|
|
assert.strictEqual(typeof to, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-03-31 09:48:01 -07:00
|
|
|
getBackupCredentials(apiConfig, function (error, credentials) {
|
2015-09-21 14:02:00 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var params = {
|
2016-03-31 09:48:01 -07:00
|
|
|
Bucket: apiConfig.bucket, // target bucket
|
|
|
|
|
Key: apiConfig.prefix + '/' + to, // target file
|
|
|
|
|
CopySource: apiConfig.bucket + '/' + apiConfig.prefix + '/' + from, // source
|
2015-09-21 14:02:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var s3 = new AWS.S3(credentials);
|
2015-09-21 14:14:21 -07:00
|
|
|
s3.copyObject(params, callback);
|
2015-09-21 14:02:00 -07:00
|
|
|
});
|
|
|
|
|
}
|