Skip to content

Instantly share code, notes, and snippets.

@tailtq
Last active October 14, 2020 10:05
Show Gist options
  • Save tailtq/4a146fa7c8af37e92e3b91a083cfac9c to your computer and use it in GitHub Desktop.
Save tailtq/4a146fa7c8af37e92e3b91a083cfac9c to your computer and use it in GitHub Desktop.
Syntax for MinIO and MongoEngine

Setup MinIO and MongoEngine

(Move to next part if you just want to know MinIO and MongoEngine syntax)

These services were setting up using docker-compose. Please refer to this link for the its content.

Setup steps:

  • Download file from the link above and save as docker-compose.yaml
  • Change environment variables for:
    • Mongodb (MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORD)
    • MinIO (MINIO_ACCESS_KEY, MINIO_SECRET_KEY)
  • Change ports of physical machine to map ports of new containers. E.g: 11038:27017 change the left port
  • Change volume path to mount container's data to our physical machine. E.g: /database/mongodb:/data change the left path
  • Install docker and docker-compose if your computer doesn't have them
  • Run docker-compose up (add sudo if needed)

(You can deploy and run this on your server)

Let's code

  • Install minio for Python: pip3 install minio
  • Install mongoengine for Python: pip3 install mongoengine

MinIO

Connect to MinIO server

from minio import Minio

minioClient = Minio('host:port',
                    access_key='AKIAIOSFODNN7EXAMPLE',
                    secret_key='wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
                    secure=False)

Create bucket

minioClient.make_bucket('local')  # bucket name

Set policy for MinIO bucket

import json

config = {
    "Version": "2012-10-17",
    "Statement": [  # uncomment if you allow anonymous users to access to your resource
        # {
        #     "Effect": "Allow",
        #     "Principal": {"AWS": ["*"]},
        #     "Action": ["s3:GetObject"],
        #     "Resource": ["arn:aws:s3:::local/*"]
        # }
    ]
}
minioClient.set_bucket_policy('local', json.dumps(config))

Put object to MinIO server by url


minioClient.fput_object('local',  # bucket name
                        'video.MP4',  # file path
                        'video.MP4',  # destination path
                        content_type='video/mp4')

Put existing numpy image to server

import io
import cv2
import PIL.Image as Image

img = cv2.imread('inference/images/bus.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
out_img = io.BytesIO()
img.save(out_img, format='jpeg')
out_img.seek(0)

minioClient.put_object('local',  # bucket name
                       'test.jpg',  # destination path
                       out_img,  # image content
                       length=out_img.getbuffer().nbytes,  # size of image
                       content_type='image/jpeg')

MongoEngine

from mongoengine import *


# database configuration
database_name = 'traffic_flows'
username = 'root'
password = 'example'
port = 11038
connection_url = 'mongodb://{}:{}@core.greenlabs.ai'.format(username, password)


# connect to mongodb
connect(database_name, host=connection_url, port=port, authentication_source='admin')


# define schema (to create collection - table in sql)
class Camera(Document):
    name = StringField(required=True)
    url = StringField(max_length=50)
    createdAt = DateTimeField()

    meta = {'collection': 'cameras'}  # collection should have a plural form


# Init and save new document
camera = Camera(name='test', url='hello world')
camera.save()


# Query document
Camera.objects(name='Test')  # finding condition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment