Transforms with Object Lambda
MinIO’s Object Lambda enables developers to programmatically transform objects on demand. You can transform objects as needed for your use case, such as redacting personally identifiable information (PII), enriching data with information from other sources, or converting between formats.
Overview
An Object Lambda handler is a small code module that transforms the contents of an object and returns the results. Like Amazon S3 Object Lambda functions, you trigger a MinIO Object Lambda handler function with a GET request from an application. The handler retrieves the requested object from MinIO, transforms it, and returns the modified data back to MinIO to send to the original application. The original object remains unchanged.
Each handler is an independent process, and multiple handlers can transform the same data. This allows you to use the same object for different purposes without maintaining different versions of the original.
Object Lambda Handlers
You can write a handler function in any language capable of sending and receiving HTTP requests. It must be able to:
- Listen for an HTTP POST request.
- Retrieve the original object using a URL.
- Return the transformed contents and authorization tokens.
Create a Function
A handler function should perform the following steps:
-
Extract the object details from the incoming POST request.
The
getObjectContextproperty of the JSON request payload contains details about the original object. To construct the response, you need the following values:Value Description inputS3UrlA presigned URL for the original object. The calling application generates the URL and sends it in the original request. This allows the handler to access the original object without the MinIO credentials usually required. The URL is valid for one hour. outputRouteA token that allows MinIO to validate the destination for the transformed object. Return this value with the response in an x-amz-request-routeheader.outputTokenA token that allows MinIO to validate the response. Return this value in the response in an x-amz-request-tokenheader. -
Retrieve the original object from MinIO.
Use the presigned URL to retrieve the object from the MinIO deployment. The contents of the object are in the body of the response.
-
Transform the object as desired.
Perform any operations needed to generate a transformed object. Since the calling application is waiting for a response, you may wish to avoid potentially long running operations.
-
Construct a response containing the following information:
- The transformed object contents.
- An
x-amz-request-routeheader with theoutputRoutetoken. - An
x-amz-request-tokenheader with theoutputTokentoken.
-
Return the response back to Object Lambda.
MinIO validates the response and sends the transformed data back to the original calling application.
Response headers
Handlers must include the outputRoute and outputToken values in the appropriate response headers. This allows MinIO to correctly validate the response from the handler.
Register the Handler
To enable MinIO to call the handler, register the handler function as a webhook with the following MinIO server Object Lambda environment variables:
MINIO_LAMBDA_WEBHOOK_ENABLE_functionname
Enable or disable Object Lambda for a handler function. For multiple handlers, set this environment variable for each function name.
MINIO_LAMBDA_WEBHOOK_ENDPOINT_functionname
Register an endpoint for a handler function. For multiple handlers, set this environment variable for each function endpoint.
MinIO also supports the following environment variables for authenticated webhook endpoints:
MINIO_LAMBDA_WEBHOOK_AUTH_TOKEN_functionanme
Specify the opaque string or JWT authorization token for authenticating to the webhook.
MINIO_LAMBDA_WEBHOOK_CLIENT_CERT_functionname
Specify the client certificate to use for mTLS authentication to the webhook.
MINIO_LAMBDA_WEBHOOK_CLIENT_KEY_functionname
Specify the private key to use for mTLS authentication to the webhook.
Restart MinIO to apply the changes.
Alternatively, configure Object Lambda with the MinIO Client command line tool. For more information, see Object Lambda function settings.
Trigger From an Application
To request a transformed object from your application:
-
Connect to the MinIO deployment.
-
Set the Object Lambda target by adding a
lambdaArnparameter with the ARN of the desired handler. -
Generate a presigned URL for the original object.
-
Use the generated URL to retrieve the transformed object.
MinIO sends the request to the target Object Lambda handler. The handler returns the transformed contents back to MinIO, which validates the response and sends it back to the application.
Example
Transform the contents of an object using Python, Go, and curl:
- Create and register an Object Lambda handler.
- Create a bucket and an object to transform.
- Request and display the transformed object contents.
Prerequisites:
- An existing MinIO deployment
- Working Python (3.8+) and Golang development environments
- The MinIO Go SDK
Create a Handler
The sample handler, written in Python, retrieves the target object using a presigned URL generated by the caller. The handler then transforms the object’s contents and returns the new text. It uses the Flask web framework and Python 3.8+.
The following command installs Flask and other needed dependencies:
The handler calls swapcase() to change the case of each letter in the original text. It then sends the results back to MinIO, which returns it to the caller.
Start the Handler
Use the following command to start the handler in your local development environment:
The output resembles the following:
Start MinIO
Once the handler is running, start MinIO with the MINIO_LAMBDA_WEBHOOK_ENABLE and MINIO_LAMBDA_WEBHOOK_ENDPOINT environment variables to register the function with MinIO. To identify the specific Object Lambda handler, append the name of the function to the name of the environment variable.
The following command starts MinIO in your local development environment:
Replace myfunction with the name of your handler function and /data with the location of the MinIO directory for your local deployment. The output resembles the following:
Test the Handler
To test the Lambda handler function, first create an object to transform. Then invoke the handler, in this case with curl, using the presigned URL from a Go function.
-
Create a bucket and object for the handler to transform.
-
Invoke the Handler
The following Go code uses the The MinIO Go SDK to generate a presigned URL and print it to
stdout.In the code above, replace the following values:
- Replace
my_admin_userandmy_admin_passwordwith user credentials for a MinIO deployment. - Replace
myfunctionwith the same function name set in theMINIO_LAMBDA_WEBHOOK_ENABLEandMINIO_LAMBDA_WEBHOOK_ENDPOINTenvironment variables.
To retrieve the transformed object, execute the Go code with
curlto generate a GET request:curlruns the Go code and then retrieves the object with a GET request to the presigned URL. The output resembles the following: - Replace