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 =
__write_count = def __init__(self):
pass @staticmethod
def get_write_instance():
if ImageFeatIO.__write_singleton is None:
ImageFeatIO.__write_count +=
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 +=
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('\n','').split('=')
print(s, len(s))
paras[s[]] = s[]
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 文件为参数配置文件, 不同参数以回车键分割

分布式存储——ceph 的 python 基础接口的更多相关文章

  1. Python基础-接口与归一化设计、抽象类、继承顺序、子类调用父类,多态与多态性

    一.接口与归一化设计 Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能). 由 ...

  2. python基础--接口与归一化设计、封装、异常、网络编程

    1 接口与归一化设计 1.1 归一化概念: 归一化的好处: 1.归一化让使用者无需关心对象的类是什么,只需要知道这些对象都具备某些功能就可以了,这极大降低了使用者的使用难度. 2.归一化使得高层的外部 ...

  3. python基础一之课后作业:编写登录接口

    1 # Author : Mamba 2 3 #python基础一之课后作业:编写登录接口 4 5 # 输入用户名密码 6 # 认证成功后显示欢迎信息 7 # 用户名3次输入错误后,退出程序 8 # ...

  4. python基础之继承派生、组合、接口和抽象类

    类的继承与派生 经典类和新式类 在python3中,所有类默认继承object,但凡是继承了object类的子类,以及该子类的子类,都称为新式类(在python3中所有的类都是新式类) 没有继承obj ...

  5. Py修行路 python基础 (十五)面向对象编程 继承 组合 接口和抽象类

    一.前提回忆: 1.类是用来描述某一类的事物,类的对象就是这一类事物中的一个个体.是事物就要有属性,属性分为 1:数据属性:就是变量 2:函数属性:就是函数,在面向对象里通常称为方法 注意:类和对象均 ...

  6. python基础练习-猜年龄、编写登陆接口小程序

    python基础练习:   一.猜年龄 , 可以让用户最多猜三次! age=40 count = 1 while count <=3 : user_guess=int(input("i ...

  7. 全能成熟稳定开源分布式存储Ceph破冰之旅-上

    @ 目录 概述 定义 传统存储方式及问题 优势 生产遇到问题 架构 总体架构 组成部分 CRUSH算法 数据读写过程 CLUSTER MAP 部署 部署建议 部署版本 部署方式 Cephadm部署 前 ...

  8. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  9. Python之路3【第一篇】Python基础

    本节内容 Python简介 Python安装 第一个Python程序 编程语言的分类 Python简介 1.Python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum) ...

随机推荐

  1. C#控制台打开VM虚拟机

    添加引用->VixCOM.dll (在vix文件夹下) VixWrapper.cs using System; using System.Collections.Generic; using S ...

  2. Excel催化剂开源第31波-pdf相关功能实现及类库介绍

    在Excel催化剂刚推出的pdf相关功能中,反馈很热烈,不止是用户层面好多人喜欢,也听到在.NET开发群里有询问pdf在winform上展现的功能诉求,一段时间没写开源篇,生怕大家以为Excel催化剂 ...

  3. hadoop之安装hadoop

    官网 http://hadoop.apache.org/ 下载页:https://hadoop.apache.org/releases.html 上传安装包到Linux 解压并进入到目录下 [root ...

  4. pyqt 主程序运行中处理其他事件(多线程的一种代替方式)

    一.实验环境 1.Windows7x64_SP1 2.Anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 4.通过Anacond ...

  5. libvirt-qemu-TLS加密虚拟机传输实例分析

    说明 TLS(Transport Layer Security Protocol),即安全传输层协议,其核心是加密两台计算机之间的通信.libvirt中使用TLS,可以实现libvirt的安全加密.可 ...

  6. github项目readme.md文件如何编写

    参考链接:http://blog.csdn.net/Bone_ACE/article/details/48318675

  7. go mod 无法自动下载依赖包的问题

    go 11以后启用了go mod功能,用于管理依赖包. 当执行go mod init生成go.mod文件之后,golang在运行.编译项目的时候,都会检查依赖并下载依赖包. 在启动了go mod之后, ...

  8. Linux基础之定时任务

    30.1)什么是定时任务 定时任务命令是cond,crond就是计划任务,类似于我们平时生活中的闹钟,定点执行. 30.2)为什么要用crond 计划任务主要是做一些周期性的任务,比如凌晨3点定时备份 ...

  9. [学习笔记] pymysql入门

    一.快速开始 对于会用MySQL的朋友来说,开始使用pymysql可以说真的so esay,只要用下面的代码,把想要对数据库的操作放在 sql = " " 里就可以了. 没有接触过 ...

  10. RabbitMQ搭建单机及集群

    1,基本环境配置 hosts 文件 免密登录 2,访问官网 https://www.rabbitmq.com/download.html 3, 4,安装依赖 yum -y install make g ...