fromflaskimportFlask,request,abort,make_responseimportrequestsapp=Flask(__name__)@app.route('/',methods=['POST'])defget_webhook():ifrequest.method=='POST':# Get the request event from the 'POST' callevent=request.json# Get the object contextobject_context=event["getObjectContext"]# Get the presigned URL# Used to fetch the original object from MinIOs3_url=object_context["inputS3Url"]# Extract the route and request tokens from the input contextrequest_route=object_context["outputRoute"]request_token=object_context["outputToken"]# Get the original S3 object using the presigned URLr=requests.get(s3_url)original_object=r.content.decode('utf-8')# Transform the text in the object by swapping the case of each chartransformed_object=original_object.swapcase()# Return the object back to Object Lambda, with required headers# This sends the transformed data to MinIO# and then to the userresp=make_response(transformed_object,200)resp.headers['x-amz-request-route']=request_routeresp.headers['x-amz-request-token']=request_tokenreturnrespelse:abort(400)if__name__=='__main__':app.run()
启动 Handler
使用以下命令在本地开发环境中启动 handler:
python lambda_handler.py
输出类似如下:
* Serving Flask app 'lambda_handler' * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
packagemainimport("context""log""net/url""time""fmt""github.com/minio/minio-go/v7""github.com/minio/minio-go/v7/pkg/credentials")funcmain(){// Connect to the MinIO deployments3Client,err:=minio.New("localhost:9000",&minio.Options{Creds:credentials.NewStaticV4("my_admin_user","my_admin_password",""),Secure:false,})iferr!=nil{log.Fatalln(err)}// Set the Lambda function target using its ARNreqParams:=make(url.Values)reqParams.Set("lambdaArn","arn:minio:s3-object-lambda::myfunction:webhook")// Generate a presigned url to access the original objectpresignedURL,err:=s3Client.PresignedGetObject(context.Background(),"myfunctionbucket","testobject",time.Duration(1000)*time.Second,reqParams)iferr!=nil{log.Fatalln(err)}// Print the URL to stdoutfmt.Println(presignedURL)}