s3.js/test.js

315 lines
8.4 KiB
JavaScript
Raw Permalink Normal View History

2020-03-12 10:26:31 +00:00
'use strict';
require('dotenv').config();
var env = process.env;
var s3 = require('./index.js');
var accessKeyId = env.AWS_ACCESS_KEY || env.AWS_ACCESS_KEY_ID;
2020-03-12 10:26:31 +00:00
var secretAccessKey = env.AWS_SECRET_ACCESS_KEY;
var region = env.AWS_REGION;
var bucket = env.AWS_BUCKET;
var prefix = env.AWS_BUCKET_PREFIX;
var key = 'test-file';
var fs = require('fs');
async function run() {
// UPLOAD
//var testFile = __filename;
var testFile = 'test.bin';
var stat = fs.statSync(testFile);
var size = stat.size;
var stream = fs.createReadStream(testFile);
var file = fs.readFileSync(testFile);
await s3
.set({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key,
body: stream,
size
})
2021-01-26 23:48:15 +00:00
.then(function (resp) {
2020-03-12 10:26:31 +00:00
console.info('PASS: stream uploaded file');
return null;
})
2021-01-26 23:48:15 +00:00
.catch(function (err) {
2020-03-12 10:26:31 +00:00
console.error('Error:');
console.error('PUT Response:');
if (err.response) {
console.error(err.response.statusCode);
console.error(err.response.headers);
console.error(
(err.response.body && err.response.body) ||
JSON.stringify(err.response.body)
);
} else {
console.error(err);
}
process.exit(1);
});
// CHECK DOES EXIST
await s3
.head({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key
})
2021-01-26 23:48:15 +00:00
.then(function (resp) {
2020-03-12 10:26:31 +00:00
console.info('PASS: streamed file exists');
return null;
})
2021-01-26 23:48:15 +00:00
.catch(function (err) {
2020-03-12 10:26:31 +00:00
console.error('HEAD Response:');
if (err.response) {
console.error(err.response.statusCode);
console.error(err.response.headers);
} else {
console.error(err);
}
process.exit(1);
});
// GET STREAMED FILE
await s3
.get({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key
})
2021-01-26 23:48:15 +00:00
.then(function (resp) {
2020-03-12 10:26:31 +00:00
if (file.toString('binary') === resp.body.toString('binary')) {
console.info(
'PASS: streamed file downloaded with same contents'
);
return null;
}
throw new Error("file contents don't match");
})
2021-01-26 23:48:15 +00:00
.catch(function (err) {
2020-03-12 10:26:31 +00:00
console.error('Error:');
console.error('GET Response:');
if (err.response) {
console.error(err.response.statusCode);
console.error(err.response.headers);
} else {
console.error(err);
}
process.exit(1);
});
// DELETE TEST FILE
await s3
.del({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key
})
2021-01-26 23:48:15 +00:00
.then(function (resp) {
2020-03-12 10:26:31 +00:00
console.info('PASS: delete file');
return null;
})
2021-01-26 23:48:15 +00:00
.catch(function (err) {
2020-03-12 10:26:31 +00:00
console.error('Error:');
console.error('DELETE Response:');
if (err.response) {
console.error(err.response.statusCode);
console.error(err.response.headers);
} else {
console.error(err);
}
process.exit(1);
});
// SHOULD NOT EXIST
await s3
.head({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key
})
2021-01-26 23:48:15 +00:00
.then(function (resp) {
2020-03-12 10:26:31 +00:00
var err = new Error('file should not exist');
err.response = resp;
throw err;
})
2021-01-26 23:48:15 +00:00
.catch(function (err) {
2020-03-12 10:26:31 +00:00
if (err.response && 404 === err.response.statusCode) {
console.info('PASS: streamed file deleted');
return null;
}
console.error('Error:');
console.error('HEAD Response:');
if (err.response) {
console.error(err.response.statusCode);
console.error(err.response.headers);
} else {
console.error(err);
}
process.exit(1);
});
// CREATE WITHOUT STREAM
await s3
.set({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key,
body: file
})
2021-01-26 23:48:15 +00:00
.then(function (resp) {
2020-03-12 10:26:31 +00:00
console.info('PASS: one-shot upload');
return null;
})
2021-01-26 23:48:15 +00:00
.catch(function (err) {
2020-03-12 10:26:31 +00:00
console.error('Error:');
console.error('PUT Response:');
if (err.response) {
console.error(err.response.statusCode);
console.error(err.response.headers);
console.error(
(err.response.body && err.response.body) ||
JSON.stringify(err.response.body)
);
} else {
console.error(err);
}
process.exit(1);
});
// CHECK DOES EXIST
await s3
.head({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key
})
2021-01-26 23:48:15 +00:00
.then(function (resp) {
2020-03-12 10:26:31 +00:00
console.info('PASS: one-shot upload exists');
return null;
})
2021-01-26 23:48:15 +00:00
.catch(function (err) {
2020-03-12 10:26:31 +00:00
console.error('Error:');
console.error('HEAD Response:');
if (err.response) {
console.error(err.response.statusCode);
console.error(err.response.headers);
} else {
console.error(err);
}
process.exit(1);
});
// GET ONE-SHOT FILE
await s3
.get({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key
})
2021-01-26 23:48:15 +00:00
.then(function (resp) {
2020-03-12 10:26:31 +00:00
if (file.toString('binary') === resp.body.toString('binary')) {
console.info(
'PASS: one-shot file downloaded with same contents'
);
return null;
}
throw new Error("file contents don't match");
})
2021-01-26 23:48:15 +00:00
.catch(function (err) {
2020-03-12 10:26:31 +00:00
console.error('Error:');
console.error('GET Response:');
if (err.response) {
console.error(err.response.statusCode);
console.error(err.response.headers);
} else {
console.error(err);
}
process.exit(1);
});
// DELETE FILE
await s3
.del({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key
})
2021-01-26 23:48:15 +00:00
.then(function (resp) {
2020-03-12 10:26:31 +00:00
console.info('PASS: DELETE');
return null;
})
2021-01-26 23:48:15 +00:00
.catch(function (err) {
2020-03-12 10:26:31 +00:00
console.error('Error:');
console.error('DELETE Response:');
if (err.response) {
console.error(err.response.statusCode);
console.error(err.response.headers);
} else {
console.error(err);
}
process.exit(1);
});
// SHOULD NOT EXIST
await s3
.head({
accessKeyId,
secretAccessKey,
region,
bucket,
prefix,
key
})
2021-01-26 23:48:15 +00:00
.then(function (resp) {
2020-03-12 10:26:31 +00:00
var err = new Error('file should not exist');
err.response = resp;
throw err;
})
2021-01-26 23:48:15 +00:00
.catch(function (err) {
2020-03-12 10:26:31 +00:00
if (err.response && 404 === err.response.statusCode) {
console.info('PASS: streamed file deleted');
return null;
}
console.error('Error:');
console.error('HEAD Response:');
if (err.response) {
console.error(err.response.statusCode);
console.error(err.response.headers);
} else {
console.error(err);
}
process.exit(1);
});
}
run();