refactored server
This commit is contained in:
parent
6d470b8eba
commit
3fd2537311
36 changed files with 2944 additions and 792 deletions
51
server/storage/s3.js
Normal file
51
server/storage/s3.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
const AWS = require('aws-sdk');
|
||||
const s3 = new AWS.S3();
|
||||
|
||||
class S3Storage {
|
||||
constructor(config, log) {
|
||||
this.bucket = config.s3_bucket;
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
async length(id) {
|
||||
const result = await s3
|
||||
.headObject({ Bucket: this.bucket, Key: id })
|
||||
.promise();
|
||||
return result.ContentLength;
|
||||
}
|
||||
|
||||
getStream(id) {
|
||||
return s3.getObject({ Bucket: this.bucket, Key: id }).createReadStream();
|
||||
}
|
||||
|
||||
async set(id, file) {
|
||||
let hitLimit = false;
|
||||
const upload = s3.upload({
|
||||
Bucket: this.bucket,
|
||||
Key: id,
|
||||
Body: file
|
||||
});
|
||||
file.on('limit', () => {
|
||||
hitLimit = true;
|
||||
upload.abort();
|
||||
});
|
||||
try {
|
||||
await upload.promise();
|
||||
} catch (e) {
|
||||
if (hitLimit) {
|
||||
throw new Error('limit');
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
del(id) {
|
||||
return s3.deleteObject({ Bucket: this.bucket, Key: id }).promise();
|
||||
}
|
||||
|
||||
ping() {
|
||||
return s3.headBucket({ Bucket: this.bucket }).promise();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = S3Storage;
|
Loading…
Add table
Add a link
Reference in a new issue