python连接ceph集群获取存储桶中的图片

ceph的一般操作
1、连接

import boto.s3.connection
access_key = '***'
secret_key = '***'
ip = '***'
port = ***
conn=boto.connect_s3(
        aws_access_key_id=access_key,
        aws_secret_access_key=secret_key,
        host=ip,port=port,
        is_secure=False,
        calling_format=boto.s3.connection.OrdinaryCallingFormat())

2、遍历所有桶

for bucket in conn.get_all_buckets():
        print("{name}\t{created}".format(
                name = bucket.name,
                created = bucket.creation_date,
        ))

3、访问该桶

bucket = conn.get_bucket('桶信息')
#输出该桶内的内容
for key in bucket.list():
    print(key.name)

从ceph集群中获取图片信息如下代码:

#读取桶中的图片内容
import boto.s3.connection

class CONNECTION(object):
    def __init__(self,access_key,secret_key,ip,port,is_secure=False,chrunksize=8<<20): #chunksize最小8M否则上传过程会报错
        self.conn=boto.connect_s3(
        aws_access_key_id=access_key,
        aws_secret_access_key=secret_key,
        host=ip,port=port,
        is_secure=is_secure,
        calling_format=boto.s3.connection.OrdinaryCallingFormat()
        )
        self.chrunksize=chrunksize
        self.port=port
        
    def generate_object_download_urls(self,key_name,bucket_name,valid_time=0):
        #下载生成的URL
        all_bucket_name_list = [i.name for i in self.conn.get_all_buckets()]
        if bucket_name not in all_bucket_name_list:
            print('Bucket %s is not exist,please try again' % (bucket_name))
            return
        else:
            bucket = self.conn.get_bucket(bucket_name)

        all_key_name_list = [i.name for i in bucket.get_all_keys()]
        if key_name not in all_key_name_list:
            print('File %s is not exist,please try again' % (key_name))
            return
        else:
            key = bucket.get_key(key_name)

        try:
            key.set_canned_acl('public-read')
            download_url = key.generate_url(valid_time, query_auth=False, force_http=True)
            if self.port != 80:
                x1=download_url.split('/')[0:3]
                x2=download_url.split('/')[3:]
                s1=u'/'.join(x1)
                s2=u'/'.join(x2)

                s3=':%s/' %(str(self.port))
                download_url=s1+s3+s2
                print(download_url)

        except Exception:
            pass
if __name__ == '__main__':
    #约定:
    #1:bucket_name 相当于文件在对象存储中的目录名或者索引名
    #2:key_name 相当于文件在对象存储中对应的文件名或文件索引
    # 账户
    access_key = '***'
    secret_key = '**'
    ip = '**'
    port = ****
    bucket_name = '桶名称'
    conn=CONNECTION(access_key,secret_key,ip,port)
   
    #生成下载url
    conn.generate_object_download_urls(key_name,bucket_name)

对于生成的图片url进行解析成图片,进行下方操作

#加载url解析图片内容
import requests
from io import BytesIO
from PIL import Image

r =requests.get(url) 
curr_img = Image.open(BytesIO(r.content))
curr_img_cv2 =cv2.cvtColor(np.array(curr_img), cv2.COLOR_RGB2BGR)

#如果想看图片的样子,添加下图即可浏览
cv2.imshow("image", curr_img_cv2) # 显示图片
cv2.waitKey(0) #等待按键

你可能感兴趣的:(python碎片车轮代码,python,ceph,开发语言)