zoukankan      html  css  js  c++  java
  • 分布式存储——ceph 的 python 基础接口

    python 使用 boto 库完成分布式存储读、写、判断接口

    import boto
    import boto.s3.connection
    from boto.s3.key import Key
    import os
    
    
    class ImageFeatIO:
        __read_singleton = None
        __write_singleton = None
        __read_count = 0
        __write_count =0
    
        def __init__(self):
            pass
    
        @staticmethod
        def get_write_instance():
            if ImageFeatIO.__write_singleton is None:
                ImageFeatIO.__write_count += 1
                print("create write instance:{0}",ImageFeatIO.__write_count)
                paras = get_config('config')
                access_key = paras['access_key']
                secret_key =paras['secret_key']
                write_host=paras['file_write_host']
                conn = boto.connect_s3(
                    aws_access_key_id=access_key,
                    aws_secret_access_key=secret_key,
                    host=write_host, is_secure=False,
                    calling_format=boto.s3.connection.OrdinaryCallingFormat()
                )
                bucket_name = paras['bucket_name']
                bucket = conn.get_bucket(bucket_name)
                ImageFeatIO.__write_singleton=bucket
            return ImageFeatIO.__write_singleton
    
        @staticmethod
        def get_read_instance():
            if ImageFeatIO.__read_singleton is None:
                ImageFeatIO.__read_count += 1
                print("create read instance:{0}", ImageFeatIO.__read_count)
                paras = get_config('config')
                access_key = paras['access_key']
                secret_key = paras['secret_key']
                read_host = paras['file_read_host']
                conn = boto.connect_s3(
                    aws_access_key_id=access_key,
                    aws_secret_access_key=secret_key,
                    host=read_host, is_secure=False,
                    calling_format=boto.s3.connection.OrdinaryCallingFormat()
                )
                bucket_name = paras['bucket_name']
                bucket = conn.get_bucket(bucket_name)
                ImageFeatIO.__write_singleton = bucket
            return ImageFeatIO.__read__singleton
    
    
    def write_image_feature_to_file(id, imageFeaturestring):
        bucket = ImageFeatIO.get_write_instance()
        k = Key(bucket)
        k.key = id
        res = k.set_contents_from_string(imageFeaturestring)
        return res
    
    
    def read_image_feature_from_file(id):
        bucket = ImageFeatIO.get_write_instance()
        k = Key(bucket)
        k.key=id
        feature = k.get_contents_as_string()
        return feature
    
    
    def if_image_feature_exist(id):
        bucket = ImageFeatIO.get_write_instance()
        key = bucket.get_key(id)
    
        return key is not None
    
    
    def get_config(config_file='config'):
        paras = dict()
        if os.path.exists(config_file):
            f = open(config_file)
            line = f.readline()
            while line:
                # print('line is ', line)
                s = line.replace(' ', '').replace('
    ','').split('=')
                print(s, len(s))
                paras[s[0]] = s[1]
                line = f.readline()
            # print(' paras: ', paras)
            f.close()
        else:
            raise ValueError(config_file + ': file not exsit!')
        return paras
    
    # if __name__ == "__main__":
    #     instance1 = ImageFeatIO.get_write_instance()
    #     instance2 = ImageFeatIO.get_write_instance()
    #     instance3 = ImageFeatIO.get_write_instance()
    #
    #
    #     print id(instance1)
    #     print id(instance2)

    其中 config 文件为参数配置文件, 不同参数以回车键分割

  • 相关阅读:
    MySQL Connector Net 6.6.5 Entity Framework 显式预加载 Eager Load Bug
    MySQL 5.6 Replication 复制 FAQ
    MySQL 实现 EF Code First TimeStamp/RowVersion 并发控制
    Linux 系统命令备忘
    MySQL 自增列插入0值的方法
    【转载】将数据存储到SmartObject中
    【转载】使用C#代码发起K2 Blackpearl流程
    【转载】WorkFlow、BPM、SOA
    百度统计喻友平:谈如何更好的优化网站质量
    【转载】浅识K2 blackpearl中SmartObject
  • 原文地址:https://www.cnblogs.com/yaolin1228/p/9064540.html
Copyright © 2011-2022 走看看