-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (59 loc) · 2 KB
/
index.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
const express = require('express');
const fs = require('fs');
const { Storage } = require('@google-cloud/storage');
const projectId = process.env.projectId //Alter for local testing
const keyFilename = './som-rit-ourvoice-cloud-storage-key.json';
const app = express();
var storage;
if (fs.existsSync(keyFilename))
storage = new Storage({ projectId, keyFilename });
else
storage = new Storage({ projectId });
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
/**
* HTTP Cloud Function.
*
* @param {Object} req Cloud Function request context.
* More info: https://expressjs.com/en/api.html#req
* @param {Object} res Cloud Function response context.
* More info: https://expressjs.com/en/api.html#res
*/
app.post('/', async (req, res, next) => {
try {
const { bucketName, fileName, projectAbv, walkHash } = req.body;
const unixTimestamp = new Date().getTime();
const storageLocation = `${projectAbv}/${walkHash}/${unixTimestamp}/${fileName}`;
const url = await generateV4UploadSignedUrl(bucketName, storageLocation);
res.status(200).send({ 'signedUrl': url });
/*
File will be produced at bucketName/projectAbv/walkHash/CurrentTime
*/
} catch (err) {
next(err);
}
});
async function generateV4UploadSignedUrl(bucketName, storageLocation) {
// These options will allow temporary uploading of the file with outgoing
// Content-Type: application/octet-stream header.
const options = {
version: 'v4',
action: 'write',
expires: Date.now() + 15 * 60 * 1000, // 15 minutes
contentType: 'audio/x-wav',
};
// Get a v4 signed URL for uploading file
const [url] = await storage
.bucket(bucketName)
.file(storageLocation)
.getSignedUrl(options);
// console.log('You can use this URL with any user agent, for example:');
// console.log(
// "curl -X PUT -H 'Content-Type: audio/x-wav' " +
// `--upload-file my-file '${url}'`
// );
return url;
}
module.exports = {
app
};