memory-card
Version:
ES6 Map like Async API, with Swagger API Backend Support.
69 lines • 1.91 kB
JavaScript
import AWS_SDK from 'aws-sdk';
import { log, } from '../config.js';
import { StorageBackend, } from './backend.js';
const { S3 } = AWS_SDK;
class StorageS3 extends StorageBackend {
s3;
constructor(name, options) {
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) {
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() {
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() {
log.verbose('StorageS3', 'destroy()');
const options = this.options;
await this.s3.deleteObject({
Bucket: options.bucket,
Key: this.name,
}).promise();
}
}
export default StorageS3;
export { StorageS3, };
//# sourceMappingURL=s3.js.map