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. mybatis的example类

    1. 场景描述 idea下使用mybatis_generator自动生成mapper文件,默认生成了一大堆的example文件及方法,使用规则类似于Hibernate,给了一大堆参数,感觉没必要,只所 ...

  2. Netty-Channel架构体系源码解读

    全文围绕下图,Netty-Channel的简化版架构体系图展开,从顶层Channel接口开始入手,往下递进,闲言少叙,直接开撸 概述: 从图中可以看到,从顶级接口Channel开始,在接口中定义了一套 ...

  3. pycharm install python packaging tools时遇到AttributeError: '_NamespacePath' object has no attribute 'sort'错误

    pycharm install python packaging tools时报错AttributeError: '_NamespacePath' object has no attribute 's ...

  4. g++ -std=c++11 -g -o test emit_log_direct.cpp

    g++ -std=c++11 -g -o test  emit_log_direct.cpp

  5. JavaScript函数及闭包

    前面一片文章讲到过一点函数,了解到每声明一个函数就会产生一个作用域.而外面的作用域访问不了里面的作用域(把里面的变量和函数隐藏起来),而里面的可以访问到外面的.对于隐藏变量和函数是一个非常有用的技术. ...

  6. JS高级程序设计第4章--精简版

    前言:纯手打!!!按照自己思路重写!!!这次是二刷了,想暑假做一次完整的笔记,但用本子来写笔记的话太贵了,可能哪天还丢了..所以还是博客好== 第四章:变量.作用域和内存问题 4.1 基本类型和引用类 ...

  7. c语言进阶8-数据结构

    一.  数据结构的起源: 1.        为什么要学习数据结构 阿基米德说过:“给我一个支点,我就能翘起地球”.那么给我一个程序,我就能用好程序,给我一个结构,我就能把内容填充完成.打个比方,一个 ...

  8. 为什么选择 Spring 作为 Java 框架

    1. 概述 在本文中,我们将讨论 Spring 作为最流行的 Java 框架之一的主要价值体现. 最重要的是,我们将尝试理解 Spring 成为我们选择框架的原因.Spring 的详细信息及其组成部分 ...

  9. PTA 打印沙漏

    https://pintia.cn/problem-sets/17/problems/260 #include <bits/stdc++.h> using namespace std; i ...

  10. fjnuoj 1004 游戏 (博弈论)

    思路: N 为1或质数 先手必赢 N 为质数X质数的积 后手必赢 先手取两个质数的积则赢 #include #include #define ll long long using namespace ...