This micro-service is used for calling Sharp pipelines on supplied images and then uploading the result to an S3 compliant endpoint or local storage. Images are streamed to a file, transformed and then streamed to the endpoint to minimize memory usage during upload.
Configuration is stored in the config.yaml file in the top directory of this project, you can override this by using the CONFIG_FILE environment variable. Additionally environment variables can be used inside the configuration file by using the spring like ${VARIABLE} syntax.
To configure uploading to aws the following YAMl could be used:
port: 8080
uploadDirectory: /tmp/uploaded_files
upload:
bucket: ${AWS_BUCKET}
key: ${AWS_KEY}
secret: ${AWS_SECRET}
endpoint: ${AWS_ENDPOINT}
pathStyle: ${AWS_PATH_STYLE}
To configure copying to a local directory the following YAMl could be used:
port: 8080
uploadDirectory: /tmp/uploaded_files
upload:
saveDirectory: /images/
To use this micro-service two variables pipeline and image need to be POST'ed to the route "/". Starting this service and a minio instance for testing can be done using docker compose.
docker-compose up
Resize an image:
curl -F "pipeline=[ { \"resize\" : [ 20, 20 ]} ]" -F "image=@/Users/me/my-image.jpg" http://127.0.0.1:8080/
Convert an uploaded image to WEBP:
curl -F "pipeline=[ { \"webp\" : {} } ]" -F "image=@/Users/me/my-image.jpg" http://127.0.0.1:8080/
Multiple stages can be defined (as with Sharp), remember the order matters!
curl -F "pipeline=[ { \"resize\" : [ 200, 200 ]}, { \"rotate\": [90] }, { \"jpeg\" : { \"quality\" : 100 } } ]" -F "image=@/Users/me/my-image.jpg" http://127.0.0.1:8080/
You can deploy the image service to a kubernetes cluster using the sample deployment configuration.
kubectl apply -f image-service.yaml
The following Sharp methods are supported, usage can be found in the Sharp docs:
- resize
- crop
- embed
- max
- min
- ignoreAspectRatio
- withoutEnlargement
- rotate
- extract
- flip
- flop
- sharpen
- median
- blur
- extend
- flatten
- trim
- gamma
- negate
- normalise
- normalize
- convolve
- threshold
- linear
- background
- greyscale
- grayscale
- toColourspace
- toColorspace
- jpeg
- png
- webp
- tiff
- raw
Variable | Usage |
---|---|
AWS_BUCKET | The S3 bucket images should be uploaded/downloaded from. |
AWS_KEY | AWS key. |
AWS_SECRET | AWS Secret. |
AWS_ENDPOINT | AWS S3 endpoint, for example http://127.0.0.1:9000 |
AWS_PATH_STYLE | Enable/disable force path style for AWS client, is required by some S3 compliant providers. |
PORT | The port this service listens on. |
UPLOAD_DIR | The directory within this container images are uploaded and transformed in. |
Testing using Apache jMeter with 100 threads, sending a 5MB file 100 times each showed the following:
CPU | RAM | Storage | Requests/s |
---|---|---|---|
2 cores of 2.9 GHz Intel Core i7 @ 100% | 100-200MB | 55B-200MB | 35~ |