Add s3 storage removeBackup() and getDownloadStream() implementations
This commit is contained in:
@@ -231,9 +231,20 @@ function removeBackup(apiConfig, backupId, appBackupIds, callback) {
|
||||
assert(Array.isArray(appBackupIds));
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
// Result: none
|
||||
getBackupCredentials(apiConfig, function (error, credentials) {
|
||||
if (error) return callback(error);
|
||||
|
||||
callback(new Error('not implemented'));
|
||||
var params = {
|
||||
Bucket: apiConfig.bucket,
|
||||
Key: getBackupFilePath(apiConfig, backupId)
|
||||
};
|
||||
|
||||
var s3 = new AWS.S3(credentials);
|
||||
s3.deleteObject(params, function (error) {
|
||||
if (error) console.error('Unable to remove %s. Not fatal.', params.Key, error);
|
||||
callback(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getDownloadStream(apiConfig, backupId, callback) {
|
||||
@@ -241,11 +252,46 @@ function getDownloadStream(apiConfig, backupId, callback) {
|
||||
assert.strictEqual(typeof backupId, 'string');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
callback = once(callback);
|
||||
|
||||
var backupFilePath = getBackupFilePath(apiConfig, backupId);
|
||||
|
||||
debug('[%s] getDownloadStream: %s %s', backupId, backupId, backupFilePath);
|
||||
|
||||
callback(new Error('not implemented'));
|
||||
getBackupCredentials(apiConfig, function (error, credentials) {
|
||||
if (error) return callback(error);
|
||||
|
||||
var params = {
|
||||
Bucket: apiConfig.bucket,
|
||||
Key: backupFilePath
|
||||
};
|
||||
|
||||
var s3 = new AWS.S3(credentials);
|
||||
|
||||
s3.headObject(params, function (error, result) {
|
||||
if (error && error.code === 'NotFound') return callback(new BackupsError(BackupsError.NOT_FOUND));
|
||||
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error));
|
||||
|
||||
var s3get = s3.getObject(params).createReadStream();
|
||||
var decrypt = crypto.createDecipher('aes-256-cbc', apiConfig.key || '');
|
||||
|
||||
s3get.on('error', function (error) {
|
||||
if (error.code === 'NoSuchKey') return callback(new BackupsError(BackupsError.NOT_FOUND));
|
||||
|
||||
console.error('[%s] getDownloadStream: s3 stream error.', backupId, error);
|
||||
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error));
|
||||
});
|
||||
|
||||
decrypt.on('error', function (error) {
|
||||
console.error('[%s] getDownloadStream: decipher stream error.', error);
|
||||
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
||||
});
|
||||
|
||||
s3get.pipe(decrypt);
|
||||
|
||||
callback(null, decrypt);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function testConfig(apiConfig, callback) {
|
||||
|
||||
Reference in New Issue
Block a user