Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

This is a mix of several repos for usage of the sharp library via serverless on AWS Lambda

License

NotificationsYou must be signed in to change notification settings

einSelbst/serverless-image-resizer

 
 

Repository files navigation

Serverless Framework-based AWS Lambda function triggered by S3 events to resize images with the excellentSharp module. By using the Sharp module (which uses the libvips library), image processing can be 3x-5x faster than using ImageMagick, thus reducing the time your function spends running, which can potentially dramatically decrease your lambda function's cost. The function's behaviour can be controlled entirely with configuration.

This is based on the following projects:

Contents

  1. What is it?
  2. Installation
  3. Setup
  4. Testing
  5. Local Invocation
  6. Deployment
  7. Logs
  8. Documentation

What is it?

A tool to take images uploaded to an S3 bucket and produce one or more images of varying sizes, optimizations and other operations. It does this by creating an AWS Lambda function with the help of theServerless Framework.It uses Docker to build the sharp library and then deploys the Lambda function from within docker.

The current configuration is to create the following versions for a supplied image:

  • original width / 6, quality 80%
  • original width / 4, quality 80%
  • original width / 3, quality 80%
  • original width / 2, quality 80%
  • original width * 2 / 3, quality 80%
  • original width, quality 80%

Installation

Installation can be achieved with the following commands:

git clone https://github.com/einselbst/serverless-image-resizercd serverless-image-resizeryarn install

Or, if you haveserverless installed globally:

serverless install -u https://github.com/einselbst/serverless-image-resizer

Then, modify theconfig.js andevent.json files, adapting them to your needs. More on configurationbelow.

Setup

Credentials

You must configure your AWS credentials by definingAWS_ACCESS_KEY_ID andAWS_SECRET_ACCESS_KEY environmental variables.

export AWS_ACCESS_KEY_ID=<your-key-here>export AWS_SECRET_ACCESS_KEY=<your-secret-key-here>

Buckets

The buckets must exist. They need to be set in theconfig.json file.When the project is deployed, the S3 trigger needs to be manually set in the AWS Lambda console:Configuration -> Add triggers -> S3 -> Choose correct source bucket -> Add -> Save. While there is aplugin for this it doesn't seem to be very mature, so it's not included yet.

Testing

Make sure the bucket inconfig.js exists.

We use Jest to run our tests. You can read more about setting up your testshere.

yarntest

You can also try out the service by invoking it. First deploy it withyarn run deploy and then you can invoke your function withyarn run invoke. This will invoke the function with the test event inevent.json. You may need to tweak this file to match your setup.

Local Invocation

To run a function on your local machine:

$ serverless invokelocal --function resizeImage

To simulate API Gateway locally usingserverless-offline

$ serverless offline start

Deployment

Deploy project to staging:

$ yarn deploy-dev

This will calldocker-compose run deploy-dev.

Deploy project to production:

$ yarn deploy

This will calldocker-compose run deploy.

Deploy a single function:

$ serverless deployfunction--function resizeImage

Delete project deployment (stack & lambda functions), pass the Stage which should be destroyed (dev/production).

$ serverless remove --STAGE dev

Logs

Prints the tailing logs for staging to the console:

$ yarn logs-dev

Prints the tailing logs for production to the console:

$ yarn logs

Documentation

image.js

  • receives the initial event from S3 in processItem
  • returns an array of promises, each promise resolves to one processed file
  • invokes the get from s3
  • invokes the sharp API
  • invokes the uploads to s3

s3.js

  • has default source and destination buckets
  • implements the get from s3
  • implements the uploads to s3

sharp.js

  • the sharp APIs

About

This is a mix of several repos for usage of the sharp library via serverless on AWS Lambda

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript93.7%
  • Dockerfile6.3%

[8]ページ先頭

©2009-2025 Movatter.jp