-
Notifications
You must be signed in to change notification settings - Fork 14
feat(url-shortener): use a Lambda function URL #331
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
Changes from all commits
41c6f73
81e6316
7e80cb5
5558d7a
1abd4f0
9cb5e23
bb9382a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3'; | ||
|
|
||
| const s3Client = new S3Client({}); | ||
|
|
||
| export async function handler(event: AWSLambda.LambdaFunctionURLEvent): Promise<AWSLambda.LambdaFunctionURLResult> { | ||
| try { | ||
| const key = event.rawPath.substring(1); // remove first slash | ||
|
|
||
| const data = await s3Client.send(new GetObjectCommand({ | ||
| Bucket: process.env.BUCKET_NAME, | ||
| Key: key, | ||
| })); | ||
|
|
||
| if (!data.Body) { | ||
| throw new Error('No body'); | ||
| } | ||
|
|
||
| const redirect = JSON.parse(await data.Body.transformToString()); | ||
|
|
||
| if (!redirect.url) { | ||
| throw new Error('Redirect object in S3 is missing a `url` property.'); | ||
| } | ||
|
|
||
| return { | ||
| statusCode: 301, | ||
| headers: { | ||
| Location: redirect.url, | ||
| }, | ||
| }; | ||
| } catch (err) { | ||
| console.log(err); | ||
| return { statusCode: 404 }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current error handling returns a
404 Not Foundfor any error that occurs within thetryblock. This can make debugging difficult, as it hides the underlying cause of the error (e.g., permissions issues, throttling, or other S3 errors). It would be better to distinguish between a 'Not Found' error (NoSuchKey) and other server-side errors. For unexpected errors, returning a500 Internal Server Errorwould be more appropriate and provide better observability.