Beispiel #1
0
 def pub_file_bucket(self, database, collection, file_name, file_property):
     client = MongoClient(self.host, self.port)
     db = client[database]
     bucket_files = GridFSBucket(db, bucket_name=collection)
     file_bucket_fs = bucket_files.open_upload_stream(
         filename=file_name, metadata=file_property)
     return file_bucket_fs
Beispiel #2
0
class DB_GridFS():
    def __init__(self):
        db = MongoClient().GEOSIM_GRIDFS
        self.fs = GridFSBucket(db)

    # @timer
    def upload(self, obj_name, filepath, metadata={'origin_hash': '_hash'}):
        grid_in = self.fs.open_upload_stream(
            obj_name, chunk_size_bytes=CHUNK_SIZE,
            metadata=metadata)
        with open(filepath, mode='rb') as dst:
            grid_in.write(dst)  # grid_in.write(file_like_obj)
        grid_in.close()  # uploaded on close

    # @timer
    def download(self, obj_name, filepath):
        with open(filepath, mode='wb') as dst:
            self.fs.download_to_stream_by_name(obj_name, dst)

    # @timer
    def delete(self, keyname):
        cur = self.fs.find({"filename": keyname})
        for item in cur:
            self.fs.delete(item._id)
Beispiel #3
0
        for item in json_obj['items']:
            image_url_list.append(item['link'])
    return image_url_list


keyword = '박보검'
image_park = call_and_print(keyword, 50)

# image to MongoDB

db = MongoClient().python_test
fs = GridFS(db)
bucket = GridFSBucket(db)

for url in image_park:
    try:
        image = urllib.request.urlopen(url).read()
        image_type = url.split(".")[-1]
        content_type = f"image/{image_type}"
        image_name = url.split("/")[-1]
        grid_in = bucket.open_upload_stream(image_name,
                                            metadata={
                                                "contentType": content_type,
                                                "type": "human"
                                            })
        grid_in.write(image)
        grid_in.close()
        print("image_type:", image_type, ":content_type:", content_type,
              ":image_name:", image_name)
    except:
        print("에러 발생")
Beispiel #4
0
class DB_GridFS(DBMongoBase):
    """Класс работы с MongoDB Gridfs    """
    def __init__(self, config_params, database):
        """Получаем базу, GridFSBucket, коллекцию
        :param config_params: dict
            словарь конфигурационных парметров
            dict(host='10.205.33.221', port=27017, username=None, password=None)
        :param database: string
            название базы данных MongoDB
        """
        # Client for a MongoDB instance
        # client = MongoClient(**config_params)
        super().__init__(config_params)
        # print(self.client.test_database)
        # Get a database by client and name
        self.db = self.client[database]
        # получаем GridFSBucket
        self.fs = GridFSBucket(self.db)
        # получаем коллекцию fs.files - стандартная коллекция GridFSBucket
        self.collection = self.db.fs.files
        print(self.collection)
        self.meta = self.client['main'].projects
        print(self.meta)

    def upload(self, obj_name, filepath, metadata={'origin_hash': '_hash'}):
        """метод загрузки данных в БД
        :param obj_name: string
            название объекта value поля filename
        :param filepath: file_object
            file_object с данными, отправляемыми в БД
        :param metadata: dict
            метаданные записываемые в БД
        return: None
        """
        # GridIn instance
        grid_in = self.fs.open_upload_stream(obj_name,
                                             chunk_size_bytes=CHUNK_SIZE,
                                             metadata=metadata)
        # with open(filepath, mode='rb') as dst:
        # Перемотаем буфер к началу
        filepath.seek(0)
        # запись данных
        grid_in.write(filepath)  # grid_in.write(file_like_obj)
        # закрываем instance при этом инициируется отправка  еще не отправленных данных
        grid_in.close()  # uploaded on close
        # обнулим буфер не удаляя объекта
        filepath.truncate(0)
        return grid_in._id

    def download(self, obj_name, filepath, version=-1):
        """

        :param obj_name: string
            название объекта value поля filename
        :param filepath: file_object
            куда будет загружены данные
        :param version:
            номер версии
            Revision numbers are defined as follows:
            0 = the original stored file
            1 = the first revision
            2 = the second revision
            etc…
            -2 = the second most recent revision
            -1 = the most recent revision
        :return: None
        """
        # with open(filepath, mode='wb') as dst:
        filepath.seek(0)
        # download_to_stream_by_name ничего не возвращает
        self.fs.download_to_stream_by_name(obj_name,
                                           filepath,
                                           revision=version)
        filepath.seek(0)

    def delete(self, keyname):
        """Удалиение документа по filename
        :param keyname: string
            filename
        :param return:
        """
        # получаем итератор ответа на запрос
        cur = self.fs.find({"filename": keyname})
        # проходим по ответам и удаляем соответствующие документы
        for item in cur:
            self.fs.delete(item._id)

    def insert_one(self, data_dict):
        return self.collection.insert_one(data_dict).inserted_id

    def insert_many(self, data_dict):
        self.collection.insert_many(data_dict)

    def find(self, collection, where, select={"_id": 0}):
        return collection.find(where, select, no_cursor_timeout=True)