中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ceph 對象存儲 創建api

發布時間:2020-07-12 10:26:38 來源:網絡 閱讀:583 作者:wuye1232588 欄目:編程語言
# -*- coding:utf-8 -*-

import boto
import boto.s3.connection
import paramiko

class Accountinfo():
    """
    用法詳見 http://docs.ceph.org.cn/man/8/radosgw-admin/
    """
    def __init__(self):
        self.hostname = '192.168.44.70'
        self.port = 22
        self.username = 'root'
        self.passwd = '123456'

    def new_connect(self):
        try:
            paramiko.util.log_to_file('paramiko.log')
            ssh = paramiko.SSHClient()
            ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(hostname='192.168.44.70',port=22,username='root',password='123456')
            return ssh

        except Exception as e:
            return 'error'

    def user_manager(self,username,flag):
        """
        為s3訪問創建radosgw用戶
        flag: c -> create,d -> delete
        """
        ssh = self.new_connect()
        if not isinstance(ssh,str):
            c_command = '/usr/bin/radosgw-admin  user create --uid="%s" --display-name="%s"' % (username, username.title())
            d_command = '/usr/bin/radosgw-admin  user rm --uid="%s"' % (username)

            if flag == 'c':
                stdin, stdout, stderr = ssh.exec_command(c_command)
                outstr = stdout.read()
                errstr = stdout.read()
                ssh.close()
                return outstr,errstr
            elif flag == 'd':
                stdin, stdout, stderr = ssh.exec_command(d_command)
                ssh.close()
                return 'delete %s success'% username
                # return stdout, stderr
            else:
                ssh.close()
                return 'flag == c or d','flag error'

        return 'connect error'

class CephS3():

    # 單例模式

    __instance = None

    def __init__(self):
        self.access_key = "BKOLF8C5319QK2UIMQ09"
        self.secret_key = "jBiFwY3LeHh78tM9W6Y8oQUM2VNIbieGVViB3wEB"
        self.host = '192.168.44.70'
        self.port = 7480
        self.conn = boto.connect_s3(
            aws_access_key_id= self.access_key,
            aws_secret_access_key= self.secret_key,
            host = self.host,
            port = self.port,
            is_secure=False,
            calling_format = boto.s3.connection.OrdinaryCallingFormat()
        )

    @staticmethod
    def get_connect():

        if CephS3.__instance:
            return CephS3.__instance
        else:
            CephS3.__instance = CephS3().conn
            return CephS3.__instance

    def list_all_buckets(self):
        con = CephS3.get_connect()
        all_buckets = con.get_all_buckets()

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

    def create_bucket(self,bucketname):
        # con = self.connect()
        con = CephS3.get_connect()
        all_bucket = con.get_all_buckets()
        all_bucket_name = [i.name for i in all_bucket]

        try:
            if bucketname not in all_bucket_name:
                bucket = con.create_bucket(bucketname)
                return 'ok'
                # print('Bucket %s create success'% bucketname)
            else:
                return 'fail'
                # print('Bucket %s already exists!' % bucketname)
        except Exception as e:
            return str(e)

    def delete_bucket(self,bucketname):

        con = CephS3.get_connect()
        all_bucket = con.get_all_buckets()
        all_bucket_name = [i.name for i in all_bucket]

        try:
            if bucketname in all_bucket_name:
                bucket = con.delete_bucket(bucketname)
                return 'ok'

            else:
                return 'fail'

        except Exception as e:
            print(str(e))

    def list_bucket_object(self,bucketname):

        con = CephS3.get_connect()
        bucket = con.get_bucket(bucketname)
        list_buckets = bucket.list()

        print("%-10s\t%-10s\t%-10s\n"%('Name','Size','Modified'))
        for key in list_buckets:
            print("{name:<10}\t{size:<10}\t{modified:<10}".format(name=key.name,size=key.size,modified=key.last_modified))

    def create_object(self,bucketname,obj_key,obj_value):
        """新建一個內容是字符串 obj_value 的文件 obj_key"""

        con = CephS3.get_connect()
        bucket = con.get_bucket(bucketname)
        list_buckets = bucket.list()
        obj_keys = [i.name for i in list_buckets]
        try:
            if obj_key not in obj_keys:
                bucket_key = bucket.new_key(obj_key)
                bucket_key.set_contents_from_string(obj_value)
                return 'ok'
            return 'exists'
        except Exception as e:
            return str(e)

    def modifi_object_acl(self,bucketname,obj_key,acl='public-read'):
        """
        修改對象的權限
        :param bucketname:
        :param obj_key:
        :param acl: public-read 公開可讀   private 私有
        :return:
        """
        # con = self.connect()
        con = CephS3.get_connect()
        bucket = con.get_bucket(bucketname)
        try:
            bucket_key = bucket.get_key(obj_key)
            bucket_key.set_canned_acl(acl)
            return 'ok'
        except Exception as e:
            return str(e)

    def make_obj_url(self,bucketname,obj_key,sign=False):
        try:

            con = CephS3.get_connect()
            bucket = con.get_bucket(bucketname)
            key = bucket.get_key(obj_key)
            if not sign:
                key_url_no_sign = key.generate_url(0,query_auth=False,force_http=True)
                return key_url_no_sign
            key_url_sign = key.generate_url(3600,query_auth=True,force_http=True)
            return key_url_sign
        except Exception as e:
            return str(e)

    def download_to_file(self,bucketname,obj_key,dest_file):
        try:
            # con = self.connect()
            con = CephS3.get_connect()
            bucket = con.get_bucket(bucketname)
            key = bucket.get_key(obj_key)
            key.get_contents_to_filename(dest_file)
            return 'ok'
        except Exception as e:
            return str(e)

    def delete_bucket_key(self,bucketname,obj_key):
        try:
            # con = self.connect()
            con = CephS3.get_connect()
            bucket = con.get_bucket(bucketname)
            bucket.delete_key(obj_key)
            return 'ok'
        except Exception as e:
            return str(e)

if __name__ == '__main__':
    cephapi = CephS3()

    url = cephapi.make_obj_url('bucket_bamboo','haproxy.txt',True)
    print(url)

    # cephapi.list_bucket_object('bucket_bamboo')

    # 創建用戶 獲取keys 信息

    # um = Accountinfo()
    # out, err = um.user_manager('wuye','c')
    #
    # if type(out) == str:
    #     res = json.loads(out)
    #     print(json.dumps(res["keys"][0],indent=2))
    # else:
    #     print(out,err)
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

汪清县| 定陶县| 岳阳县| 炎陵县| 宁南县| 荔浦县| 龙井市| 彰武县| 拉孜县| 临湘市| 门源| 和田市| 阜阳市| 敦煌市| 扎兰屯市| 娄底市| 班戈县| 承德县| 九台市| 长沙市| 平原县| 英山县| 上犹县| 怀宁县| 邳州市| 怀柔区| 浪卡子县| 湄潭县| 潍坊市| 尤溪县| 股票| 当阳市| 长阳| 垫江县| 仙居县| 宜君县| 土默特右旗| 东乌| 仪征市| 马龙县| 汉阴县|