memory-card
Version:
ES6 Map like Async API, with Swagger API Backend Support.
75 lines • 2.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StorageS3 = void 0;
const aws_sdk_1 = __importDefault(require("aws-sdk"));
const config_js_1 = require("../config.js");
const backend_js_1 = require("./backend.js");
const { S3 } = aws_sdk_1.default;
class StorageS3 extends backend_js_1.StorageBackend {
s3;
constructor(name, options) {
config_js_1.log.verbose('StorageS3', 'constructor()');
options.type = 's3';
super(name, options);
options = options;
this.s3 = new S3({
credentials: {
accessKeyId: options.accessKeyId,
secretAccessKey: options.secretAccessKey,
},
region: options.region,
});
}
toString() {
const text = [
this.constructor.name,
'<',
this.name,
'>',
].join('');
return text;
}
async save(payload) {
config_js_1.log.verbose('StorageS3', 'save()');
const options = this.options;
await this.s3.putObject({
Body: JSON.stringify(payload),
Bucket: options.bucket,
Key: this.name,
}).promise();
}
async load() {
config_js_1.log.verbose('StorageS3', 'load()');
const options = this.options;
try {
const result = await this.s3.getObject({
Bucket: options.bucket,
Key: this.name,
}).promise();
if (!result.Body) {
return {};
}
return JSON.parse(result.Body.toString());
}
catch (e) {
if (/^4/.test(e.statusCode)) {
return {};
}
throw e;
}
}
async destroy() {
config_js_1.log.verbose('StorageS3', 'destroy()');
const options = this.options;
await this.s3.deleteObject({
Bucket: options.bucket,
Key: this.name,
}).promise();
}
}
exports.StorageS3 = StorageS3;
exports.default = StorageS3;
//# sourceMappingURL=s3.js.map