-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgcs-direct.ts
31 lines (26 loc) · 963 Bytes
/
gcs-direct.ts
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
import { cors, MetaStorage, Uploadx } from '@uploadx/core';
import { GCStorage } from '@uploadx/gcs';
import { createServer } from 'http';
import { parse } from 'url';
const PORT = process.env.PORT || 3002;
const corsHandler = cors();
// Don't forget to set GCS_BUCKET and GCS_KEYFILE environment variables
const storage = new GCStorage({
clientDirectUpload: true,
maxUploadSize: '15GB',
allowMIME: ['video/*', 'image/*'],
filename: file => file.originalName,
metaStorage: new MetaStorage()
});
const uploadx = new Uploadx({ storage });
uploadx.on('created', file =>
console.log('google upload link sent to client: ', file.GCSUploadURI)
);
createServer((req, res) => {
const { pathname } = parse(req.url || '');
if (pathname === '/files') {
uploadx.handle(req, res);
} else {
corsHandler(req, res, () => uploadx.send(res, { body: 'Not Found', statusCode: 404 }));
}
}).listen(PORT, () => console.log('listening on port:', PORT));