(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
)
- Mongodb (
- 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
(addsudo
if needed)
(You can deploy and run this on your server)
- Install
minio
for Python:pip3 install minio
- Install
mongoengine
for Python:pip3 install mongoengine
from minio import Minio
minioClient = Minio('host:port',
access_key='AKIAIOSFODNN7EXAMPLE',
secret_key='wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
secure=False)
minioClient.make_bucket('local') # bucket name
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))
minioClient.fput_object('local', # bucket name
'video.MP4', # file path
'video.MP4', # destination path
content_type='video/mp4')
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')
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