storage: make exists async

This commit is contained in:
Girish Ramakrishnan
2022-04-14 08:07:03 -05:00
parent 11f7be2065
commit d54c03f0a0
6 changed files with 28 additions and 39 deletions

View File

@@ -138,10 +138,9 @@ function upload(apiConfig, backupFilePath, sourceStream, callback) {
});
}
function exists(apiConfig, backupFilePath, callback) {
async function exists(apiConfig, backupFilePath) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof backupFilePath, 'string');
assert.strictEqual(typeof callback, 'function');
const credentials = getS3Config(apiConfig);
@@ -153,13 +152,12 @@ function exists(apiConfig, backupFilePath, callback) {
Key: backupFilePath
};
s3.headObject(params, function (error) {
if (!Object.keys(this.httpResponse.headers).some(h => h.startsWith('x-amz'))) return callback(new BoxError(BoxError.EXTERNAL_ERROR, 'not a s3 endpoint'));
if (error && S3_NOT_FOUND(error)) return callback(null, false);
if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, `Error headObject ${backupFilePath}. Message: ${error.message} HTTP Code: ${error.code}`));
const [error] = await safe(s3.headObject(params).promise());
if (!Object.keys(this.httpResponse.headers).some(h => h.startsWith('x-amz'))) throw new BoxError(BoxError.EXTERNAL_ERROR, 'not a s3 endpoint');
if (error && S3_NOT_FOUND(error)) return false;
if (error) throw new BoxError(BoxError.EXTERNAL_ERROR, `Error headObject ${backupFilePath}. Message: ${error.message} HTTP Code: ${error.code}`);
callback(null, true);
});
return true;
} else { // list dir contents
const listParams = {
Bucket: apiConfig.bucket,
@@ -167,11 +165,10 @@ function exists(apiConfig, backupFilePath, callback) {
MaxKeys: 1
};
s3.listObjects(listParams, function (error, listData) {
if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, `Error listing objects ${backupFilePath}. Message: ${error.message} HTTP Code: ${error.code}`));
const [error, listData] = await safe(s3.listObjects(listParams).promise());
if (error) throw new BoxError(BoxError.EXTERNAL_ERROR, `Error listing objects ${backupFilePath}. Message: ${error.message} HTTP Code: ${error.code}`);
callback(null, listData.Contents.length !== 0);
});
return listData.Contents.length !== 0;
}
}