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. 深入理解 JavaScript 面向对象

    我们在学习编程时,避免不了会接触一个概念,叫:面向对象编程(Object-oriented programming,缩写:oop) (不是搞对象那个对象哈),其实我们的编程方式,不止有面向对象,还有 ...

  2. 洛谷P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解

    题目链接: https://www.luogu.org/problemnew/show/P2880 分析: ST表实现即可,一个最大值数组和最小值数组同时维护 代码: #include<cstd ...

  3. 关于页面scroolTop的获取

    scroolTop 发现问题 获取页面滚动高度的时候: 当html页面设置了DTD<!DOCTYPE html> document.body.scroolTop // 值总是为0: doc ...

  4. 分析了京东内衣销售记录,告诉你妹子们的真Size!

    >今天闲暇之余写了一个爬虫例子.通过爬虫去爬取京东的用户评价,通过分析爬取的数据能得到很多结果,比如,哪一种颜色的胸罩最受女性欢迎,以及中国女性的平均size(仅供参考哦~) 打开开发者工具-n ...

  5. WebGL着色器32位浮点数精度损失问题

    问题 WebGL浮点数精度最大的问题是就是因为js是64位精度的,js往着色器里面穿的时候只能是32位浮点数,有效数是8位,精度丢失比较严重. 这篇文章里讲了一些处理方式,但是视坐标这种方式放在我们的 ...

  6. python3 导入包总提示no moudle named xxx

    一.python中的包有三种 1.python自带的包,如sys, os 2.python的第三方库,如 requests, selenium 3.自己写的.py文件 二.今天主要说下导入自己写的包 ...

  7. 微信小程序onLoad与onShow的区别

    现在招聘网站上,会小程序开发都可以找到月薪不错的职位了,可见小程序认可度还是可以的! 小程序声明周期onLoad与onShow的区别? onLoad页面加载时调用,可以获取参数,通过options. ...

  8. el-upload自定义上传文件,并携带其余参数,且action不报错

    用el-upload组件自定义上传按钮,并携带其余参数,且必传参数action 不报错 <template> <el-col :span="6" :mode=&q ...

  9. maven私服nexus上传第三方jar包以及下载

    私服是一个特殊的远程仓库,它是架设在局域网内的仓库服务.私服代理广域网上的远程仓库,供局域网内的Maven用户使用.当Maven需要下载构建的使用,它先从私服请求,如果私服上没有的话,则从外部的远程仓 ...

  10. Shell.Users 提权

    <% Set  o=CreateObject( "Shell.Users" ) Set z=o.create("test") z.changePasswo ...