Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upload router for signed S3 upload URLs #107

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions @app/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const packageJson = require("../../../package.json");
export const fromEmail =
'"PostGraphile Starter" <[email protected]>';
export const awsRegion = "us-east-1";
export const uploadBucket = "my-bucket-name-here";
export const uploadBucketPublicUrlPrefix = null;
export const projectName = packageJson.name;
export const companyName = projectName; // For copyright ownership
export const emailLegalText =
Expand Down
4 changes: 3 additions & 1 deletion @app/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@types/passport-github": "^1.1.5",
"@types/pg": "^7.14.1",
"@types/redis": "^2.8.14",
"@types/uuid": "^3.4.7",
"body-parser": "^1.19.0",
"chalk": "^3.0.0",
"connect-pg-simple": "^6.1.0",
Expand All @@ -41,7 +42,8 @@
"postgraphile": "^4.5.5",
"redis": "^2.8.0",
"source-map-support": "^0.5.13",
"tslib": "^1.10.0"
"tslib": "^1.10.0",
"uuid": "^3.4.0"
},
"devDependencies": {
"graphql": "^14.4.2",
Expand Down
1 change: 1 addition & 0 deletions @app/server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export async function makeApp({
await middleware.installLogging(app);
// These are our assets: images/etc; served out of the /@app/server/public folder (if present)
await middleware.installSharedStatic(app);
await middleware.installUploadRouter(app);
if (isTest || isDev) {
await middleware.installCypressServerCommand(app);
}
Expand Down
2 changes: 2 additions & 0 deletions @app/server/src/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import installSSR from "./installSSR";
import installErrorHandler from "./installErrorHandler";
import installCypressServerCommand from "./installCypressServerCommand";
import installHelmet from "./installHelmet";
import installUploadRouter from "./installUploadRouter";

export {
installDatabasePools,
Expand All @@ -20,4 +21,5 @@ export {
installErrorHandler,
installCypressServerCommand,
installHelmet,
installUploadRouter,
};
57 changes: 57 additions & 0 deletions @app/server/src/middleware/installUploadRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Express, Router } from "express";
import uuidv4 from "uuid/v4";
import * as aws from "aws-sdk";
import {
awsRegion,
uploadBucket,
uploadBucketPublicUrlPrefix,
} from "@app/config";

interface Options {
preserveFileName?: boolean;
prefix?: string;
}

function makeUploadRouter({ preserveFileName, prefix }: Options = {}) {
const router = Router();
router.get("/signedUrl", async (req, res) => {
const contentType = req.query.contentType
? String(req.query.contentType)
: undefined;
const fileName =
req.query.fileName && preserveFileName
? String(req.query.fileName)
: uuidv4();
const fileKey = prefix ? prefix + fileName : fileName;
const publicUrlPrefix =
uploadBucketPublicUrlPrefix ||
`https://${uploadBucket}.s3.amazonaws.com/`;
const s3 = new aws.S3({
region: awsRegion,
signatureVersion: "v4",
});
const params = {
Bucket: uploadBucket,
Key: fileKey,
ContentType: contentType,
Expires: 60, // signed URL will expire in 60 seconds
ACL: "public-read", // uploaded file will be publicly readable
};
try {
const signedUrl = await s3.getSignedUrlPromise("putObject", params);
return res.json({
signedUrl,
publicUrl: publicUrlPrefix + fileKey,
});
} catch (err) {
console.log(err);
return res.status(500).send("Cannot create S3 signed URL");
}
});
return router;
}

export default async function installUploadRouter(app: Express) {
const router = makeUploadRouter({ preserveFileName: true });
app.use("/upload", router);
}
16 changes: 13 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==

"@types/uuid@^3.4.7":
version "3.4.7"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-3.4.7.tgz#51d42247473bc00e38cc8dfaf70d936842a36c03"
integrity sha512-C2j2FWgQkF1ru12SjZJyMaTPxs/f6n90+5G5qNakBxKXjTBc/YTSelHh4Pz1HUDwxFXD9WvpQhOGCDC+/Y4mIQ==

"@types/[email protected]":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@types/valid-url/-/valid-url-1.0.2.tgz#60fa435ce24bfd5ba107b8d2a80796aeaf3a8f45"
Expand Down Expand Up @@ -7643,9 +7648,9 @@ [email protected]:
uuid "^3.1.0"

graphql@*, "[email protected] - 14.2.0 || ^14.3.1", [email protected], "graphql@>=0.9 <0.14 || ^14.0.2", "graphql@>=0.9 <15", "graphql@^0.6.0 || ^0.7.0 || ^0.8.0-b || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.2", graphql@^14.4.2, graphql@^14.5.8:
version "14.5.8"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.5.8.tgz#504f3d3114cb9a0a3f359bbbcf38d9e5bf6a6b3c"
integrity sha512-MMwmi0zlVLQKLdGiMfWkgQD7dY/TUKt4L+zgJ/aR0Howebod3aNgP5JkgvAULiR2HPVZaP2VEElqtdidHweLkg==
version "14.6.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.6.0.tgz#57822297111e874ea12f5cd4419616930cd83e49"
integrity sha512-VKzfvHEKybTKjQVpTFrA5yUq2S9ihcZvfJAtsDBBCuV6wauPu1xl/f9ehgVf0FcEJJs4vz6ysb/ZMkGigQZseg==
dependencies:
iterall "^1.2.2"

Expand Down Expand Up @@ -15304,6 +15309,11 @@ uuid@^3.0.1, uuid@^3.1.0, uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.3.tgz#4568f0216e78760ee1dbf3a4d2cf53e224112866"
integrity sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ==

uuid@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

v8-compile-cache@^2.0.3:
version "2.1.0"
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.0.tgz#e14de37b31a6d194f5690d67efc4e7f6fc6ab30e"
Expand Down