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. 最新 centos7下zabbix 监控redis状态

    准备 先准备两台服务器,已经部署好了zabbix-sever和zabbix-agent zabbix-server 192.168.1.70 centos7.2 zabbix-agent 192.16 ...

  2. c++中利用宏定义简化for循环使用

    话不多说,上方法 #define _for(i,a,b) for( int i=(a); i<(b); ++i) #define _rep(i,a,b) for( int i=(a); i< ...

  3. exgcd、二元一次不定方程学习笔记

    (不会LATEX,只好用Word) ( QwQ数论好难) 再补充一点,单次询问a,b求逆元的题可以直接化简然后套用exgcd求解. 例题:https://www.luogu.org/problemne ...

  4. Ubuntu安装Sublime Text 3

    终端命令行安装: Install the GPG key: wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo a ...

  5. c语言进阶14-线性表之链表

    一.  线性表的链式存储结构 1.        顺序存储结构不足的解决办法 前面我们讲的线性表的顺序存储结构.它是有缺点的,最大的缺点就是插入和删除时需要移动大量元素,这显然就需要耗费时间.能不能想 ...

  6. C#2.0新增功能06 协变和逆变

    连载目录    [已更新最新开发文章,点击查看详细] 在 C# 中,协变和逆变能够实现数组类型.委托类型和泛型类型参数的隐式引用转换. 协变保留分配兼容性,逆变则与之相反. 以下代码演示分配兼容性.协 ...

  7. linux中type 、file、stat三者的区别

    1.type 用来查看命令类型,以区别是内部命令还是外部命令 示例:[root@localhost ~]# type cd cd 是 shell 内嵌    [root@localhost ~]# t ...

  8. 小白开学Asp.Net Core 《十》

    小白开学Asp.Net Core <十> — — Session.Cookie.Cache(老生常谈) 一.背景 在常谈Session和Cookie之前我们先来简单的了解下Http(可以说 ...

  9. 在Linux - Centos上安装Python3(上)

    必看内容 在Linux上安装Python常用的2种方法 1.Python源码编译安装,有点复杂,适合老司机 2.从EPEL/IUS仓库安装,新手建议使用些方法,比较简单,目前2019-07-31提供最 ...

  10. Selenium+java - 弹出框处理

    一.弹出框分类: 弹出框分为两种,一种基于原生JavaScript写出来的弹窗,另一种是自定义封装好的样式的弹出框,本文重点介绍原生JavaScript写出来的弹窗,另一种弹窗用click()基本就能 ...