Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Last active March 25, 2024 01:43
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ruanbekker/dc3c8d2258f66045330bed9985c3c459 to your computer and use it in GitHub Desktop.
Save ruanbekker/dc3c8d2258f66045330bed9985c3c459 to your computer and use it in GitHub Desktop.
Minio Events with Bucket Notifications

Minio Bucket Notifications

Start Minio:

docker run --rm --name minio --rm -it -p 9000:9000  minio/minio server /data
> output will display here
mc config host add myminio http://localhost:9000 x_key x_secret --api S3v4
mc mb myminio/json-files
mc admin config get myminio > config.json

append:

"webhook":{"1":{"enable":true,"endpoint":"http://192.168.0.104:8082","queueDir":"","queueLimit":0}}}

Set config:

mc admin config set myminio < config.json
mc admin service restart myminio

add event subsciption:

mc event add myminio/json-files arn:minio:sqs::1:webhook --event put --suffix .json
mc event list myminio/json-files
arn:minio:sqs::1:webhook   s3:ObjectCreated:*   Filter: suffix=".json"

Flask server:

from flask import Flask, request, jsonify
from minio import Minio

client = Minio('127.0.0.1:9000', access_key='x', secret_key='x', secure=False)
app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def main():
    payload = request.get_json()
    a = client.get_object('json-files', payload['Records'][0]['s3']['object']['key'])
    print(a.read())
    return jsonify(payload)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8082)

create payload:

echo '{"id": 001, "name": "ruan", "surname": "bekker"}' > file.json

copy to minio:

mc cp file.json myminio/json-files/234098234092834092834.json

Flask output:

192.168.0.104 - - [11/Jul/2019 23:36:07] "POST / HTTP/1.1" 200 -
{"id": 001, "name": "ruan", "surname": "bekker"}
@pfriedland
Copy link

This is better than 90% of the documenting examples I search for. Thanks !!

@ybenitezf
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment