-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3-output.js
43 lines (36 loc) · 1.02 KB
/
s3-output.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { S3 } from 'aws-sdk';
import uuidv4 from 'uuid/v4';
import { fileTimeout, signedUrlTimeout } from '../s3_expiry';
class S3Output {
constructor(bucketName) {
this._reportBuffer = '';
this._bucketName = bucketName;
}
append(str) {
this._reportBuffer = this._reportBuffer.concat(str);
}
async writeBufferToS3(downloadMode = true) {
const s3 = new S3();
const filename = `${uuidv4()}.svg`;
await s3
.putObject({
Bucket: this._bucketName,
Key: filename,
ContentType: 'image/svg+xml',
ContentDisposition: `${downloadMode && 'download;'} fileName="Chart.svg"`,
Body: Buffer.from(this._reportBuffer, 'ascii'),
Expires: fileTimeout()
})
.promise();
return s3.getSignedUrl('getObject', {
Bucket: this._bucketName,
Key: filename,
Expires: signedUrlTimeout()
});
}
async close(downloadMode) {
const reportURI = await this.writeBufferToS3(downloadMode);
return reportURI;
}
}
export default S3Output;