drop.chapril.org-firefoxsend/server/storage/s3.js

49 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-02-06 23:31:18 +01:00
const AWS = require('aws-sdk');
class S3Storage {
constructor(config, log) {
this.bucket = config.s3_bucket;
this.log = log;
2019-11-08 17:58:33 +01:00
const cfg = {};
if (config.s3_endpoint != '') {
cfg['endpoint'] = config.s3_endpoint;
2019-11-08 17:58:33 +01:00
}
cfg['s3ForcePathStyle'] = config.s3_use_path_style_endpoint;
2019-11-08 17:58:33 +01:00
AWS.config.update(cfg);
this.s3 = new AWS.S3();
2018-02-06 23:31:18 +01:00
}
async length(id) {
2019-11-08 17:58:33 +01:00
const result = await this.s3
2018-02-06 23:31:18 +01:00
.headObject({ Bucket: this.bucket, Key: id })
.promise();
return Number(result.ContentLength);
2018-02-06 23:31:18 +01:00
}
getStream(id) {
return this.s3
.getObject({ Bucket: this.bucket, Key: id })
.createReadStream();
2018-02-06 23:31:18 +01:00
}
set(id, file) {
2019-11-08 17:58:33 +01:00
const upload = this.s3.upload({
2018-02-06 23:31:18 +01:00
Bucket: this.bucket,
Key: id,
Body: file
});
file.on('error', () => upload.abort());
return upload.promise();
2018-02-06 23:31:18 +01:00
}
del(id) {
2019-11-08 17:58:33 +01:00
return this.s3.deleteObject({ Bucket: this.bucket, Key: id }).promise();
2018-02-06 23:31:18 +01:00
}
ping() {
2019-11-08 17:58:33 +01:00
return this.s3.headBucket({ Bucket: this.bucket }).promise();
2018-02-06 23:31:18 +01:00
}
}
module.exports = S3Storage;