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 construct #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix CORS issue with HTTP APIs
  • Loading branch information
t-richard committed Apr 20, 2023
commit 2473083242b501a6687557f2ff93d34f661f503a
14 changes: 11 additions & 3 deletions src/constructs/aws/Upload.ts
Original file line number Diff line number Diff line change
@@ -95,13 +95,20 @@ export class Upload extends AwsConstruct {
httpApiId: Fn.ref(this.provider.naming.getHttpApiLogicalId()),
});

const lambdaProxyIntegration = new LambdaProxyIntegration({
handler: this.function,
});

this.route = new HttpRoute(this, "Route", {
httpApi: this.httpApi,
integration: new LambdaProxyIntegration({
handler: this.function,
}),
integration: lambdaProxyIntegration,
routeKey: HttpRouteKey.with("/upload-url", HttpMethod.POST),
});
this.route = new HttpRoute(this, "CORSRoute", {
httpApi: this.httpApi,
integration: lambdaProxyIntegration,
routeKey: HttpRouteKey.with("/upload-url", HttpMethod.OPTIONS),
});
}

if (resolvedConfiguration.apiGateway === "rest") {
@@ -153,6 +160,7 @@ const crypto = require("crypto");
const s3 = new AWS.S3();

exports.handler = async (event) => {
if (event.requestContext?.http?.method === 'OPTIONS') return "";
const body = JSON.parse(event.body);
const fileName = \`tmp/\${crypto.randomBytes(5).toString('hex')}-\${body.fileName}\`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this generates a random file name to avoid collisions by adding a random hash before the submitted filename.


2 changes: 2 additions & 0 deletions test/unit/upload.test.ts
Original file line number Diff line number Diff line change
@@ -28,6 +28,8 @@ describe("upload", () => {
"uploadRouteuploadRoute2545F0B8PermissionCB079AC2",
"uploadRouteHttpIntegration02104492e88c1940a1c8d0dbac532c8091C83E5A",
httpApiRoute,
"uploadCORSRouteuploadCORSRouteA2C80313PermissionEA9DCB1F",
"uploadCORSRouteC21947AF",
]);
});
it("should create all required resources with REST API", async () => {