-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic signed url functionality
- Loading branch information
0 parents
commit eed1850
Showing
9 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
_.env | ||
.env*.local | ||
.env.dev | ||
.env | ||
.env.production | ||
.env.staging | ||
.env.development | ||
.env.master | ||
key.json | ||
key.txt | ||
sec-push.sh | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
# other branches | ||
/dev-branch | ||
|
||
# other | ||
output.txt | ||
test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 4, | ||
"semi": true, | ||
"singleQuote": false, | ||
"endOfLine": "lf" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# symposia-video | ||
|
||
To install dependencies: | ||
|
||
```bash | ||
bun install | ||
``` | ||
|
||
To run: | ||
|
||
```bash | ||
bun run index.js | ||
``` | ||
|
||
This project was created using `bun init` in bun v1.0.30. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": "0.2", | ||
"ignorePaths": [], | ||
"dictionaryDefinitions": [], | ||
"dictionaries": [], | ||
"words": [ | ||
"Filip", | ||
"Niklas" | ||
], | ||
"ignoreWords": [], | ||
"import": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "caravan", | ||
"version": "0.0.1", | ||
"description": "storage server for symposia", | ||
"scripts": { | ||
"start": "bun run src/index.ts", | ||
"dev": "bun --watch src/index.ts" | ||
}, | ||
"author": "Filip Niklas", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@google-cloud/storage": "^7.11.0", | ||
"@types/express": "^4.17.21", | ||
"@types/node": "^20.12.12", | ||
"dotenv": "^16.4.5", | ||
"express": "^4.19.2", | ||
"typescript": "^5.4.5" | ||
}, | ||
"devDependencies": { | ||
"@types/bun": "latest" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^5.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Storage, type GetSignedUrlConfig } from "@google-cloud/storage"; | ||
|
||
const storage = new Storage(); | ||
|
||
const PRIMARY_BUCKET_NAME = process.env.GCP_PRIMARY_BUCKET_NAME ?? "invalid"; | ||
const SECONDARY_BUCKET_NAME = | ||
process.env.GCP_SECONDARY_BUCKET_NAME ?? "invalid"; | ||
|
||
/** | ||
* Storage bucket reference instance. Call methods on this object to interact with the bucket. | ||
* @see https://cloud.google.com/nodejs/docs/reference/storage/latest | ||
*/ | ||
export const primaryBucket = storage.bucket(PRIMARY_BUCKET_NAME); | ||
export const secondaryBucket = storage.bucket(SECONDARY_BUCKET_NAME); | ||
|
||
// export async function TestBucket() { | ||
// const sa = await storage.getProjectId(); | ||
// console.log(sa); | ||
// const a = await storage.getServiceAccount(); | ||
// console.log(a); | ||
// } | ||
|
||
export async function gcGenerateReadSignedUrl({ | ||
fileName, | ||
id, | ||
}: { | ||
fileName: string; | ||
id: string; | ||
}) { | ||
const filePath = `video/${id}/${fileName}`; | ||
const options: GetSignedUrlConfig = { | ||
version: "v4", | ||
action: "read", | ||
expires: Date.now() + 15 * 60 * 1000, // 15 minutes | ||
}; | ||
const [url] = await primaryBucket.file(filePath).getSignedUrl(options); | ||
return url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import express, { type Express, type Request, type Response } from "express"; | ||
import dotenv from "dotenv"; | ||
import { gcGenerateReadSignedUrl } from "./bucket"; | ||
|
||
dotenv.config(); | ||
|
||
const app: Express = express(); | ||
const port = process.env.PORT || 3000; | ||
|
||
app.get("/", async (req: Request, res: Response) => { | ||
const fileName = "VID_20200103_135115.mp4"; | ||
const id = "cluvqhyly0007uwfdmg2hn33a"; | ||
const url = await gcGenerateReadSignedUrl({ fileName, id }); | ||
res.send(url); | ||
}); | ||
|
||
app.get("/:id/:filename", async (req: Request, res: Response) => { | ||
const fileName = req.params.filename; | ||
const id = req.params.id; | ||
const url = await gcGenerateReadSignedUrl({ fileName, id }); | ||
res.send(url); | ||
}); | ||
|
||
app.listen(port, () => { | ||
console.log(`Server listening on port ${port}`); | ||
}); | ||
|
||
// http://localhost:3000/cluvqhyly0007uwfdmg2hn33a/VID_20200103_135115.mp4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"compilerOptions": { | ||
// Enable latest features | ||
"lib": ["ESNext"], | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleDetection": "force", | ||
"jsx": "react-jsx", | ||
"allowJs": true, | ||
|
||
// Bundler mode | ||
"moduleResolution": "bundler", | ||
"allowImportingTsExtensions": true, | ||
"verbatimModuleSyntax": true, | ||
"noEmit": true, | ||
|
||
// Best practices | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"noFallthroughCasesInSwitch": true, | ||
|
||
// Some stricter flags (disabled by default) | ||
"noUnusedLocals": false, | ||
"noUnusedParameters": false, | ||
"noPropertyAccessFromIndexSignature": false | ||
} | ||
} |