-
Notifications
You must be signed in to change notification settings - Fork 5
/
s3.js
89 lines (73 loc) · 2.7 KB
/
s3.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const http = require("http");
const { S3Client } = require("@aws-sdk/client-s3");
const { Upload } = require('@aws-sdk/lib-storage');
// If 'pechkin' is installed as an NPM package,
// you can simply `const pechkin = require('pechkin')`
// or `import * as pechkin from 'pechkin';`
// Use the dist/esm distribution if you're using ESM modules (import)
const pechkin = require("../dist/cjs");
const s3Client = new S3Client({
credentials: {
accessKeyId: "PROVIDE_YOUR_OWN",
secretAccessKey: "PROVIDE_YOUR_OWN",
},
region: "us-east-1",
});
function uploadFileToS3(key, stream) {
const upload = new Upload({
client: s3Client,
params: {
Key: key,
Bucket: "vrjam-firstbridge",
Body: stream,
}
});
return upload.done();
}
http
.createServer(async (req, res) => {
if (req.method === 'POST') {
const { fields, files } = await pechkin.parseFormData(req, {
maxTotalFileFieldCount: Infinity,
maxFileCountPerField: Infinity,
maxTotalFileCount: Infinity
});
const results = [];
let batch = [];
let i = 0;
for await (const { filename: originalFilename, stream, field } of files) {
const key = `${i}-${originalFilename}`;
results.push(
await uploadFileToS3(key, stream)
.then(({ Location }) => ({
field,
originalFilename,
location: Location,
// Here, we expect the stream to be fully consumed by `uploadFileToS3()`,
// so there's no need to wait for the 'end'/'finish' events to obtain the correct
// byte length of the stream – bytesWritten already reached its final value.
// This is in contrast with the example in `examples/fs.js`, where we
// need to wait for the 'end'/'finish' events to obtain the correct
// byte length of the stream.
length: stream.bytesWritten,
}))
);
i++;
}
results.push(await Promise.all(batch)); // process the last batch
res.writeHead(200, { 'Content-Type': 'application/json' });
return res.end(JSON.stringify({ fields, files: results }, null, 2));
}
res.writeHead(200, { 'Content-Type': 'text/html' });
return res.end(`
<h1>Pechkin File Upload</h1>
<h2>Upload files in batches to AWS S3</h2>
<form enctype="multipart/form-data" method="post">
<div>Files 1 (multiple): <input type="file" name="file1" multiple="multiple" /></div>
<input type="submit" value="Upload" />
</form>
`);
})
.listen(8000, () => {
console.log('Send a multipart/form-data request to localhost:8000 to see Pechkin in action...');
});