storage: start migration of s3 api
This commit is contained in:
+9
-29
@@ -16,10 +16,6 @@ exports = module.exports = {
|
||||
testConfig,
|
||||
removePrivateFields,
|
||||
injectPrivateFields,
|
||||
|
||||
// Used to mock GCS
|
||||
_mockInject: mockInject,
|
||||
_mockRestore: mockRestore
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
@@ -27,23 +23,10 @@ const assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
constants = require('../constants.js'),
|
||||
debug = require('debug')('box:storage/gcs'),
|
||||
GCS = require('@google-cloud/storage').Storage,
|
||||
path = require('path'),
|
||||
safe = require('safetydance');
|
||||
|
||||
let GCS = require('@google-cloud/storage').Storage;
|
||||
|
||||
// test only
|
||||
let originalGCS;
|
||||
function mockInject(mock) {
|
||||
originalGCS = GCS;
|
||||
GCS = mock;
|
||||
}
|
||||
|
||||
function mockRestore() {
|
||||
GCS = originalGCS;
|
||||
}
|
||||
|
||||
// internal only
|
||||
function getBucket(apiConfig) {
|
||||
assert.strictEqual(typeof apiConfig, 'object');
|
||||
|
||||
@@ -56,7 +39,8 @@ function getBucket(apiConfig) {
|
||||
}
|
||||
};
|
||||
|
||||
return new GCS(gcsConfig).bucket(apiConfig.bucket);
|
||||
const gcs = constants.TEST ? new globalThis.GCSMock(gcsConfig) : new GCS(gcsConfig);
|
||||
return gcs.bucket(apiConfig.bucket);
|
||||
}
|
||||
|
||||
async function getAvailableSize(apiConfig) {
|
||||
@@ -143,17 +127,13 @@ async function copy(apiConfig, oldFilePath, newFilePath, progressCallback) {
|
||||
assert.strictEqual(typeof newFilePath, 'string');
|
||||
assert.strictEqual(typeof progressCallback, 'function');
|
||||
|
||||
function copyFile(entry, iteratorCallback) {
|
||||
var relativePath = path.relative(oldFilePath, entry.fullPath);
|
||||
async function copyFile(entry) {
|
||||
const relativePath = path.relative(oldFilePath, entry.fullPath);
|
||||
|
||||
getBucket(apiConfig).file(entry.fullPath).copy(path.join(newFilePath, relativePath), function(error) {
|
||||
if (error) debug('copyBackup: gcs copy error. %o', error);
|
||||
|
||||
if (error && error.code === 404) return iteratorCallback(new BoxError(BoxError.NOT_FOUND, 'Old backup not found'));
|
||||
if (error) return iteratorCallback(new BoxError(BoxError.EXTERNAL_ERROR, error.message));
|
||||
|
||||
iteratorCallback(null);
|
||||
});
|
||||
const [copyError] = await safe(getBucket(apiConfig).file(entry.fullPath).copy(path.join(newFilePath, relativePath)));
|
||||
if (copyError) debug('copyBackup: gcs copy error. %o', copyError);
|
||||
if (copyError && copyError.code === 404) throw new BoxError(BoxError.NOT_FOUND, 'Old backup not found');
|
||||
if (copyError) throw new BoxError(BoxError.EXTERNAL_ERROR, copyError.message);
|
||||
}
|
||||
|
||||
const batchSize = 1000;
|
||||
|
||||
Reference in New Issue
Block a user