mongodb_读取id并在Minio中查找该文件是否存在
import pymongo
from minio import Minio
from minio.error import S3Error
'''
settings = {
"ip": "mongodb://rootxxxx", # ip
"db_name": "tl-xxxx", # 数据库名字
}
self.conn = MongoClient(settings['ip'])[settings['db_name']][set_name]
'''
client = pymongo.MongoClient("mongodb://root:roxxxxx")
minio_client = Minio(
"xxxx.xxxx.cn:9000",
access_key="perxxxr",
secret_key="xxxx",
region="xxxx",
secure=False
)
mongo_config = {
"ip":"mongodb://roxxxxx",
"collections": ""
}
bucket_name="ai-lhb-tl-xxxxx"
mooogdb_name="tl-xxxx"
db = client[mooogdb_name]
collection_names = db.list_collection_names()
print("collection_names:",collection_names)
collection = db.get_collection("batch10")
total_imgs = []
i=0
j=0
for data in collection.find():
total_imgs.append(data['_id'])
image_name=data['_id']
image_name="a.jpg"
i=i+1
try:
found = minio_client.stat_object(bucket_name, image_name)
if found:
j=j+1
pass
else:
print("File does not exist")
except S3Error as err:
print(err)
print("total_imgs:",len(total_imgs))
print("i:",i)
print("j:",j)
print(db.list_collection_names())
mongodb_修改Collection名称
import pymongo
'''
settings = {
"ip": "mongodb://root:root@x.x.x.100:xxxx", # ip
"db_name": "tl-stage1", # 数据库名字
}
self.conn = MongoClient(settings['ip'])[settings['db_name']][set_name]
'''
client = pymongo.MongoClient("mongodb://xxxxxx")
db = client["xxxxx"]
collection_names = db.list_collection_names()
print("collection_names:",collection_names)
collection = db.get_collection("batch10_dirty")
print(db.list_collection_names())
mongodb 复制一份已经存在的Collection并重新命名
'''**************************************************************************
文件说明:mongodb 复制一份已经存在的Collection并重新命名
版本:1.0
内容:mongodb 复制一份已经存在的Collection并重新命名
时间:2024.3.23
作者:狄云
***************************************************************************'''
import os
import sys
from os.path import expanduser
from pymongo import MongoClient
import datetime
from tqdm import tqdm
import argparse
import cv2
import numpy as np
import json
import sys
import os
import multiprocessing
import torch
from pathlib import Path
from minio import Minio
'''**************************************************************************
配置参数
***************************************************************************'''
mongo_config = {
"ip":"mongodb://xxx@xxxxxx:xx",
"collections": ""
}
LABEL_PAHT="/xxx/xxx/xxxxx/label_temp/ad8c1504-xx-11ed-8bb1-xxx/2024/01/30/1706622185/label"
'''**************************************************************************
函数名称: parse_args
描 述: 解析配置文件
输 入:
输 出:
返 回 值:
作 者:狄云
编写时间:2024.3.23
***************************************************************************'''
def parse_args(in_args=None):
parser = argparse.ArgumentParser(
description="upload stage1 imgs to mongodb and minio")
parser.add_argument(
"--collection",
default="xxxx",
help="collection name in mongodb",
)
parser.add_argument(
"--database",
default='tl-xxxx',
help="database name in mongodb",
)
return parser.parse_args(in_args)
if __name__ == '__main__':
args = parse_args()
t = datetime.datetime.now()
conn = MongoClient(mongo_config['ip'])[args.database]
print("conn=",conn)
collection_names = conn.list_collection_names()
print("collection_names=",collection_names)
if args.collection not in collection_names:
print("collection not in mongodb")
print("请检察mongodb \n1、登陆账号是新数据库还是老数据库\n2、请检查database是否正确")
exit(0)
else:
print("OK!!!! clips in mongodb")
mongo_collection = conn[args.collection]
old_collection_name = args.collection
new_collection_name = "batch10_temp"
pipeline = [
{"$match": {}},
{"$out": new_collection_name}
]
result = conn[old_collection_name].aggregate(pipeline)
print(f"已复制集合 {old_collection_name} 到 {new_collection_name}")
上传红绿灯标注json文件
将标注完成后的红绿灯json文件上传到minio,并在mogodb中更信json文件名称
'''**************************************************************************
文件说明:将标注完成后的红绿灯json文件上传到minio,并在mogodb中更信json文件名称
版本:1.0
内容:将标注完成后的红绿灯json文件上传到minio,并在mogodb中更信json文件名称
时间:2024.3.23
作者:狄云
***************************************************************************'''
import os
import sys
from os.path import expanduser
from pymongo import MongoClient
import datetime
from tqdm import tqdm
import argparse
import cv2
import numpy as np
import json
import sys
import os
import multiprocessing
import torch
from pathlib import Path
from minio import Minio
'''**************************************************************************
配置参数
***************************************************************************'''
LABEL_PAHT="/disk1/xx/xx/xxx/xxx-d038-11ed-8bb1-ebe380642749/2024/01/30/1706622185/label"
mongo_config = {
"ip":"mongodb://xxx@xxxx:xxx",
"collections": ""
}
minio_client = Minio(
"ossapi.xxx.cn:9000",
access_key="xx-xx",
secret_key="xx",
region="shjd-xx",
secure=False
)
bucket_name = "ai-xxx-tl-xx"
def parse_args(in_args=None):
parser = argparse.ArgumentParser(
description="upload stage1 imgs to mongodb and minio")
parser.add_argument(
"--collection",
default="xx",
help="collection name in mongodb",
)
parser.add_argument(
"--database",
default='tl-xxx',
help="database name in mongodb",
)
return parser.parse_args(in_args)
'''**************************************************************************
函数名称: parse_args
描 述: 使用 MongoDB 和 MinIO 来查找文件
输 入:
输 出:
返 回 值:
作 者:狄云
编写时间:2024.3.23
***************************************************************************'''
def processing():
global mongo_collection
total_imgs = []
for data in mongo_collection.find():
image_id = data['_id']
total_imgs.append(image_id)
json_name=os.path.splitext(image_id)[0]
json_name=json_name+'.json'
json_file_path = LABEL_PAHT + "/" + json_name
if os.path.exists(json_file_path):
pass
else:
print(f"The file {json_name} does not exist in the folder.")
continue
mongo_collection.update_one({"_id":image_id},{"$set":{"label_json":json_name}})
try:
minio_client.fput_object(bucket_name, json_name, json_file_path)
print(f"文件 {json_name} 上传成功至存储桶 {bucket_name}")
except Exception as e:
print(f"发生错误:{e}")
print("total_imgs=",total_imgs[0])
print("total_imgs.size=",len(total_imgs))
if __name__ == '__main__':
args = parse_args()
t = datetime.datetime.now()
conn = MongoClient(mongo_config['ip'])[args.database]
print("conn=",conn)
collection_names = conn.list_collection_names()
print("collection_names=",collection_names)
if args.collection not in collection_names:
print("collection not in mongodb")
print("请检察mongodb \n1、登陆账号是新数据库还是老数据库\n2、请检查database是否正确")
exit(0)
else:
print("OK!!!! clips in mongodb")
mongo_collection = conn[args.collection]
processing()