12/11/2023 0 Comments Aws image resizeWe call mini_magick to resize the image, and then upload the file again to the target bucket. ![]() It retrieves the S3 file, stores it in a tmp-file. write def upload_file(target_bucket, target_object) Require " mini_magick " class UploadedFile def self. Now we'll implement the UploadedFile class. I can test the om_s3(bucket_name, object_name) without having to mock the entire S3 Lambda event. This makes it easier to use the code in other contexts and also allows easier testing. This class is independent from the lambda, and in general all business logic should be. This class retrieves the s3 object, resizes it and then uploads it to another bucket. Then it calls into the not yet implemented UploadedFile class. The process function retrieves the bucket and key of the newly created object from the event object. The ImageHandler.process is the function called by the lambda and has two arguments, the event, in our case an event sent by S3 about newly created files, and the context, basically metadata about the function and its environment. upload_file( " resized-your-images ", " resized_ " + event ) process( event:, context:)īucket_name = event Require 'uploaded_file' class ImageHandler def self. We also add an S3 resource resized-your-images, this will be the bucket where we moved the resized images to. Furthermore, under the provider key, we grant the function access to S3 and use the ruby2.5 runtime. We add a function handle_resize that calls the ImageHandler.process function in handler.rb whenever an object is created in the your-images S3 bucket. We will first change the serverless.yml configuration. It will create two files, a handler.rb, the file with the actual function the Lambda will call, and a serverless.yml a file that the serverless framework uses to configure and provision the lambda and affiliated services such as AWS S3 and AWS API Gateway. This creates the basic boilerplate of a serverless framework ruby Lambda function. Serverless create -t aws-ruby -p image_resizer ![]() ![]() The first step is to install serverless npm install -g serverless The goal is that when an image is uploaded to an S3 bucket, a Lambda is started, it resizes the image, and then uploads it to another bucket. I started off with the serverless framework as this is an easy way provision Lambda functions. I was eager to try this out, Ruby's powerful syntax and features are a joy to work with and coupling this with AWS Lambda I figured it could be leveraged for some easy image resizing Lambda. ☰ Menu Using serverless Ruby on AWS Lambda to resize imagesĪt the last AWS ReInvent, it was announced that AWS Lambda would support Ruby as a runtime language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |