Files
cloudron-box/src/storage/s3.js

228 lines
7.6 KiB
JavaScript
Raw Normal View History

2015-08-24 11:13:21 -07:00
'use strict';
exports = module.exports = {
upload: upload,
download: download,
copy: copy,
removeMany: removeMany,
2017-01-04 16:22:58 -08:00
backupDone: backupDone,
2017-04-18 19:15:56 +02:00
testConfig: testConfig,
// Used to mock AWS
_mockInject: mockInject,
_mockRestore: mockRestore
2015-08-24 11:13:21 -07:00
};
2017-04-20 15:35:52 +02:00
var assert = require('assert'),
AWS = require('aws-sdk'),
BackupsError = require('../backups.js').BackupsError,
debug = require('debug')('box:storage/s3'),
PassThrough = require('stream').PassThrough,
path = require('path'),
S3BlockReadStream = require('s3-block-read-stream');
2015-08-24 11:13:21 -07:00
2017-04-18 19:15:56 +02:00
// test only
var originalAWS;
function mockInject(mock) {
originalAWS = AWS;
AWS = mock;
}
function mockRestore() {
AWS = originalAWS;
}
// internal only
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 = {
signatureVersion: apiConfig.signatureVersion || 'v4',
2016-12-07 10:47:06 +01:00
s3ForcePathStyle: true,
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
};
2016-12-07 10:47:06 +01:00
if (apiConfig.endpoint) credentials.endpoint = apiConfig.endpoint;
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
// storage api
function upload(apiConfig, backupFilePath, sourceStream, callback) {
2016-09-16 11:21:08 +02:00
assert.strictEqual(typeof apiConfig, 'object');
2017-09-19 20:40:38 -07:00
assert.strictEqual(typeof backupFilePath, 'string');
assert.strictEqual(typeof sourceStream, 'object');
2016-09-16 11:21:08 +02:00
assert.strictEqual(typeof callback, 'function');
debug('upload: %s', backupFilePath);
2015-08-25 10:01:04 -07:00
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 params = {
2016-04-04 11:44:24 -07:00
Bucket: apiConfig.bucket,
Key: backupFilePath,
Body: sourceStream
2015-08-25 10:01:04 -07:00
};
var s3 = new AWS.S3(credentials);
2017-04-27 11:40:18 -07:00
// s3.upload automatically does a multi-part upload. we set queueSize to 1 to reduce memory usage
s3.upload(params, { partSize: 10 * 1024 * 1024, queueSize: 1 }, function (error) {
if (error) {
debug('[%s] upload: s3 upload error.', backupFilePath, error);
2017-04-20 19:27:12 -07:00
return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
}
callback(null);
});
2015-08-25 10:01:04 -07:00
});
}
function download(apiConfig, backupFilePath, callback) {
assert.strictEqual(typeof apiConfig, 'object');
2017-09-19 20:40:38 -07:00
assert.strictEqual(typeof backupFilePath, 'string');
assert.strictEqual(typeof callback, 'function');
debug('download: %s', backupFilePath);
getBackupCredentials(apiConfig, function (error, credentials) {
if (error) return callback(error);
var params = {
Bucket: apiConfig.bucket,
Key: backupFilePath
};
var s3 = new AWS.S3(credentials);
2017-04-18 16:44:49 +02:00
var ps = new PassThrough();
var multipartDownload = new S3BlockReadStream(s3, params, { blockSize: 64 * 1024 * 1024, logCallback: debug });
multipartDownload.on('error', function (error) {
// TODO ENOENT for the mock, fix upstream!
if (error.code === 'NoSuchKey' || error.code === 'ENOENT') {
ps.emit('error', new BackupsError(BackupsError.NOT_FOUND));
} else {
debug('[%s] download: s3 stream error.', backupFilePath, error);
ps.emit('error', new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
}
});
multipartDownload.pipe(ps);
callback(null, ps);
});
}
2017-09-19 20:40:38 -07:00
function copy(apiConfig, oldFilePath, newFilePath, callback) {
2016-03-31 09:48:01 -07:00
assert.strictEqual(typeof apiConfig, 'object');
2017-09-19 20:40:38 -07:00
assert.strictEqual(typeof oldFilePath, 'string');
assert.strictEqual(typeof newFilePath, 'string');
2015-09-21 14:02:00 -07:00
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 = {
Bucket: apiConfig.bucket,
2017-09-19 20:40:38 -07:00
Key: newFilePath,
CopySource: path.join(apiConfig.bucket, oldFilePath)
2015-09-21 14:02:00 -07:00
};
var s3 = new AWS.S3(credentials);
2017-04-19 13:20:24 +02:00
s3.copyObject(params, function (error) {
2017-04-20 19:27:12 -07:00
if (error && error.code === 'NoSuchKey') return callback(new BackupsError(BackupsError.NOT_FOUND, 'Old backup not found'));
if (error) {
2017-09-18 14:25:46 -07:00
debug('copy: s3 copy error.', error);
2017-04-20 19:27:12 -07:00
return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
}
callback(null);
});
2015-09-21 14:02:00 -07:00
});
}
2017-09-19 20:40:38 -07:00
function removeMany(apiConfig, filePaths, callback) {
assert.strictEqual(typeof apiConfig, 'object');
2017-09-19 20:40:38 -07:00
assert(Array.isArray(filePaths));
assert.strictEqual(typeof callback, 'function');
getBackupCredentials(apiConfig, function (error, credentials) {
if (error) return callback(error);
var params = {
Bucket: apiConfig.bucket,
Delete: {
Objects: [ ] // { Key }
}
};
2017-09-19 20:40:38 -07:00
filePaths.forEach(function (filePath) {
params.Delete.Objects.push({ Key: filePath });
});
var s3 = new AWS.S3(credentials);
2017-04-23 22:09:05 -07:00
s3.deleteObjects(params, function (error, data) {
if (error) debug('removeMany: Unable to remove %s. Not fatal.', params.Key, error);
else debug('removeMany: Deleted: %j Errors: %j', data.Deleted, data.Errors);
2017-04-23 22:09:05 -07:00
callback(null);
});
});
}
function testConfig(apiConfig, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof callback, 'function');
2017-04-20 17:23:31 -07:00
if (typeof apiConfig.accessKeyId !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'accessKeyId must be a string'));
if (typeof apiConfig.secretAccessKey !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'secretAccessKey must be a string'));
if (typeof apiConfig.bucket !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'bucket must be a string'));
if (typeof apiConfig.prefix !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'prefix must be a string'));
if ('signatureVersion' in apiConfig && typeof apiConfig.prefix !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'signatureVersion must be a string'));
if ('endpoint' in apiConfig && typeof apiConfig.prefix !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'endpoint must be a string'));
// attempt to upload and delete a file with new credentials
getBackupCredentials(apiConfig, function (error, credentials) {
if (error) return callback(error);
var params = {
Bucket: apiConfig.bucket,
Key: path.join(apiConfig.prefix, 'cloudron-testfile'),
Body: 'testcontent'
};
var s3 = new AWS.S3(credentials);
s3.putObject(params, function (error) {
2017-04-20 17:23:31 -07:00
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
var params = {
Bucket: apiConfig.bucket,
Key: path.join(apiConfig.prefix, 'cloudron-testfile')
};
s3.deleteObject(params, function (error) {
2017-04-20 17:23:31 -07:00
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2017-04-18 16:51:54 +02:00
callback();
});
});
});
}
2017-01-04 16:22:58 -08:00
2017-04-21 10:31:43 +02:00
function backupDone(backupId, appBackupIds, callback) {
assert.strictEqual(typeof backupId, 'string');
assert(Array.isArray(appBackupIds));
2017-01-04 16:22:58 -08:00
assert.strictEqual(typeof callback, 'function');
callback();
}