forked from hull-ships/hull-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.js
More file actions
39 lines (34 loc) · 951 Bytes
/
s3.js
File metadata and controls
39 lines (34 loc) · 951 Bytes
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
/**
* Configure the streaming to AWS.
*/
import Stream from "stream";
import Aws from "aws-sdk";
export function upload(users, shipId, partNumber) {
// Convert users array to stream
const stream = new Stream.Readable();
users.forEach(user => stream.push(`${JSON.stringify(user)}\n`));
stream.push(null);
const Body = new Stream.PassThrough();
const Bucket = process.env.BUCKET_PATH;
const Key = `extracts/${shipId}/${new Date().getTime()}-${partNumber}.json`;
const params = {
Bucket, Key, Body,
ACL: "private",
ContentType: "application/json",
};
return new Promise((resolve, reject) => {
const s3 = new Aws.S3();
s3.upload(params, (err) => {
if (err) {
reject(err);
} else {
resolve({
url: s3.getSignedUrl("getObject", { Bucket, Key, Expires: 86400 }),
partNumber,
size: users.length,
});
}
});
stream.pipe(Body);
});
}