Skip to content

Add lambda function for image resizing#946

Merged
NiallJoeMaher merged 4 commits into
codu-code:developfrom
NiallJoeMaher:feat/upload-lambdas
Jul 4, 2024
Merged

Add lambda function for image resizing#946
NiallJoeMaher merged 4 commits into
codu-code:developfrom
NiallJoeMaher:feat/upload-lambdas

Conversation

@NiallJoeMaher
Copy link
Copy Markdown
Contributor

✨ Codu Pull Request 💻

Codu Logo

Pull Request details

  • Add lambda function for image resizing. This creates a set of images optimized for responsiveness.
  • Update avatar resize lambda to now override existing image with optimized image.

Any Breaking changes

  • None, not in use yet.

@NiallJoeMaher NiallJoeMaher requested a review from a team as a code owner July 4, 2024 09:32
@vercel
Copy link
Copy Markdown

vercel Bot commented Jul 4, 2024

@NiallJoeMaher is attempting to deploy a commit to the Codú Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 4, 2024

Warning

Rate limit exceeded

@NiallJoeMaher has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 17 minutes and 3 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 28e134d and aa07e67.

Walkthrough

These changes include removing an intermediary function from an existing Lambda to simplify its logic, adding a new Lambda function for resizing images uploaded to an S3 bucket, and updating the AWS CDK stack to reflect these changes. The stack now manages two distinct Lambda functions: one for avatar resizing and another for general image uploads.

Changes

File Change Summary
cdk/lambdas/avatarResize/index.js Removed the createTargetKey function and directly used the key value in the PutObjectCommand call.
cdk/lambdas/uploadResize/index.js Introduced a new Lambda function to resize images uploaded to an S3 bucket.
cdk/lib/storage-stack.ts Renamed the existing Lambda function for resizing uploads and added a new Lambda function for avatar uploads. Configured specific entry points, dependencies, and S3 event sources.

Sequence Diagram(s)

sequenceDiagram
  participant S3
  participant Lambda (Avatar Resize)
  participant Lambda (Upload Resize)
  
  S3-->>Lambda (Avatar Resize): Object Created (avatar/)
  Lambda (Avatar Resize)-->>S3: Upload resized versions
  
  S3-->>Lambda (Upload Resize): Object Created (public/)
  Lambda (Upload Resize)-->>S3: Upload resized versions
Loading

Poem

🎨 Beneath the sky, where codes align,
The avatars dance, the images shine.
Functions weave their resizing tale,
Streams of S3 never fail.
🎉 Cheers to the code, updates so sweet,
Our Lambda world, an intricate feat! 🐰


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Outside diff range and nitpick comments (1)
cdk/lambdas/avatarResize/index.js (1)

Line range hint 8-33:
Consider optimizing the stream handling for large images.

Using Buffer.concat(await stream.toArray()) might not be optimal for large images. Consider using streams to handle large images more efficiently.

-    const resizedImage = await sharp(Buffer.concat(await stream.toArray()))
+    const resizedImage = await sharp(stream)
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9d9506f and 28e134d.

Files ignored due to path filters (4)
  • cdk/lambdas/avatarResize/package-lock.json is excluded by !**/package-lock.json, !**/*.json
  • cdk/lambdas/avatarResize/package.json is excluded by !**/*.json
  • cdk/lambdas/uploadResize/package-lock.json is excluded by !**/package-lock.json, !**/*.json
  • cdk/lambdas/uploadResize/package.json is excluded by !**/*.json
Files selected for processing (3)
  • cdk/lambdas/avatarResize/index.js (2 hunks)
  • cdk/lambdas/uploadResize/index.js (1 hunks)
  • cdk/lib/storage-stack.ts (1 hunks)
Additional comments not posted (5)
cdk/lambdas/avatarResize/index.js (2)

Line range hint 1-6:
LGTM!

The imports and S3 client initialization are appropriate and necessary for the functionality.


Line range hint 34-43:
LGTM!

The upload of the resized image and the success response handling are correctly implemented.

cdk/lambdas/uploadResize/index.js (2)

1-18: LGTM!

The imports, S3 client initialization, and size definitions are appropriate and necessary for the functionality.


36-75: LGTM!

The resizing, uploading, and success response handling are correctly implemented.

cdk/lib/storage-stack.ts (1)

Line range hint 1-61:
LGTM!

The imports and class definition are appropriate and necessary for the functionality.

Comment thread cdk/lambdas/uploadResize/index.js Outdated
Comment on lines +20 to +35
exports.handler = async (event) => {
const bucket = event.Records[0].s3.bucket.name;
const key = event.Records[0].s3.object.key;

const params = {
Bucket: bucket,
Key: key,
};

try {
const response = await s3.send(new GetObjectCommand(params));
const stream = response.Body;

if (!stream) throw new Error("BodyStream is empty");

const imageBuffer = Buffer.concat(await stream.toArray());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider optimizing the stream handling for large images.

Using Buffer.concat(await stream.toArray()) might not be optimal for large images. Consider using streams to handle large images more efficiently.

-    const imageBuffer = Buffer.concat(await stream.toArray());
+    const imageBuffer = await stream;

Committable suggestion was skipped due to low confidence.

Comment thread cdk/lib/storage-stack.ts
Comment on lines +63 to +75
// Lambda for resizing avatar uploads
const s3AvatarEventHandler = new NodejsFunction(this, "ResizeAvatar", {
runtime: lambda.Runtime.NODEJS_20_X,
entry: path.join(__dirname, "/../lambdas/imageResize/index.js"),
timeout: cdk.Duration.seconds(120),
depsLockFilePath: path.join(
__dirname,
"/../lambdas/avatarResize/package-lock.json",
),
bundling: {
nodeModules: ["sharp", "@aws-sdk/client-s3"],
},
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fix the entry point path for the Lambda function.

The entry point path for the Lambda function should be ../lambdas/avatarResize/index.js instead of ../lambdas/imageResize/index.js.

-      entry: path.join(__dirname, "/../lambdas/imageResize/index.js"),
+      entry: path.join(__dirname, "/../lambdas/avatarResize/index.js"),
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Lambda for resizing avatar uploads
const s3AvatarEventHandler = new NodejsFunction(this, "ResizeAvatar", {
runtime: lambda.Runtime.NODEJS_20_X,
entry: path.join(__dirname, "/../lambdas/imageResize/index.js"),
timeout: cdk.Duration.seconds(120),
depsLockFilePath: path.join(
__dirname,
"/../lambdas/avatarResize/package-lock.json",
),
bundling: {
nodeModules: ["sharp", "@aws-sdk/client-s3"],
},
});
// Lambda for resizing avatar uploads
const s3AvatarEventHandler = new NodejsFunction(this, "ResizeAvatar", {
runtime: lambda.Runtime.NODEJS_20_X,
entry: path.join(__dirname, "/../lambdas/avatarResize/index.js"),
timeout: cdk.Duration.seconds(120),
depsLockFilePath: path.join(
__dirname,
"/../lambdas/avatarResize/package-lock.json",
),
bundling: {
nodeModules: ["sharp", "@aws-sdk/client-s3"],
},
});

Comment thread cdk/lib/storage-stack.ts Outdated
Comment on lines +78 to +106
const s3UploadEventHandler = new NodejsFunction(this, "ResizeAvatar", {
runtime: lambda.Runtime.NODEJS_20_X,
entry: path.join(__dirname, "/../lambdas/imageResize/index.js"),
timeout: cdk.Duration.seconds(120),
depsLockFilePath: path.join(
__dirname,
"/../lambdas/imageResize/package-lock.json",
"/../lambdas/avatarResize/package-lock.json",
),
bundling: {
nodeModules: ["sharp", "@aws-sdk/client-s3"],
},
});

this.bucket.grantReadWrite(s3EventHandler);
this.bucket.grantReadWrite(s3AvatarEventHandler);
this.bucket.grantReadWrite(s3UploadEventHandler);

s3EventHandler.addEventSource(
s3AvatarEventHandler.addEventSource(
new S3EventSource(this.bucket, {
events: [s3.EventType.OBJECT_CREATED],
filters: [{ prefix: "u/" }],
}),
);

s3UploadEventHandler.addEventSource(
new S3EventSource(this.bucket, {
events: [s3.EventType.OBJECT_CREATED],
filters: [{ prefix: "public/" }],
}),
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fix the entry point path for the Lambda function.

The entry point path for the Lambda function should be ../lambdas/uploadResize/index.js instead of ../lambdas/imageResize/index.js.

-      entry: path.join(__dirname, "/../lambdas/imageResize/index.js"),
+      entry: path.join(__dirname, "/../lambdas/uploadResize/index.js"),
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const s3UploadEventHandler = new NodejsFunction(this, "ResizeAvatar", {
runtime: lambda.Runtime.NODEJS_20_X,
entry: path.join(__dirname, "/../lambdas/imageResize/index.js"),
timeout: cdk.Duration.seconds(120),
depsLockFilePath: path.join(
__dirname,
"/../lambdas/imageResize/package-lock.json",
"/../lambdas/avatarResize/package-lock.json",
),
bundling: {
nodeModules: ["sharp", "@aws-sdk/client-s3"],
},
});
this.bucket.grantReadWrite(s3EventHandler);
this.bucket.grantReadWrite(s3AvatarEventHandler);
this.bucket.grantReadWrite(s3UploadEventHandler);
s3EventHandler.addEventSource(
s3AvatarEventHandler.addEventSource(
new S3EventSource(this.bucket, {
events: [s3.EventType.OBJECT_CREATED],
filters: [{ prefix: "u/" }],
}),
);
s3UploadEventHandler.addEventSource(
new S3EventSource(this.bucket, {
events: [s3.EventType.OBJECT_CREATED],
filters: [{ prefix: "public/" }],
}),
);
const s3UploadEventHandler = new NodejsFunction(this, "ResizeAvatar", {
runtime: lambda.Runtime.NODEJS_20_X,
entry: path.join(__dirname, "/../lambdas/uploadResize/index.js"),
timeout: cdk.Duration.seconds(120),
depsLockFilePath: path.join(
__dirname,
"/../lambdas/avatarResize/package-lock.json",
),
bundling: {
nodeModules: ["sharp", "@aws-sdk/client-s3"],
},
});
this.bucket.grantReadWrite(s3AvatarEventHandler);
this.bucket.grantReadWrite(s3UploadEventHandler);
s3AvatarEventHandler.addEventSource(
new S3EventSource(this.bucket, {
events: [s3.EventType.OBJECT_CREATED],
filters: [{ prefix: "u/" }],
}),
);
s3UploadEventHandler.addEventSource(
new S3EventSource(this.bucket, {
events: [s3.EventType.OBJECT_CREATED],
filters: [{ prefix: "public/" }],
}),
);

@NiallJoeMaher NiallJoeMaher merged commit ca7fcef into codu-code:develop Jul 4, 2024
@NiallJoeMaher NiallJoeMaher deleted the feat/upload-lambdas branch July 4, 2024 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant