当前位置: 首页>编程语言>正文

swift pod文件全局定义 swift 文档

1、背景知识

OpenStack Object Storage (Swift) 是用来创建冗余的、可扩展的对象存储(引擎)的开源软件。 swift使用标准化的服务器存储 PB 级的访问数据。它并不是文件系统或者实时的数据存储系统 ,而是一个用于存储永久类型的静态数据的长期存储系统 ,这些数据可以检索、调整、必要时进行更新。最适合存储的数据类型的首要例子是虚拟机镜像,图片存储,邮件存储,存档备份。因为没有中心单元或者主控结点,swift提供了更强的扩展性、冗余和持久性。

OpenStack Object Storage (Swift)使用RESTful API对外提供服务,通过HTTP协议来进行数据间的交互,数据存放在request中。Swift支持单个无文件最大5G,大于5G的文件在客户端切分上传,并上传manifest文件。

 

图 1-1 SWIFT数据交互

 

2、API使用描述

1)获取token

接口名称:

获取一个token ID

接口定义:

curl -X POST -d '

{"auth":

        {"tenantName": “{租户名}”,

         "passwordCredentials": {

            "username": “{用户名}”,

            "password": “{用户密码}”}

        }

}'

-H "Content-type: application/json" http://localhost:35357/v2.0/tokens | python -mjson.tool

 

传入参数定义:

{"tenantName": “{租户名}”,

     "passwordCredentials": {

     "username": “{用户名}”,

     "password": “{用户密码}”}

}

 

返回结果

返回的结果比较冗长,请自己在实验环境192.168.0.191上操作,并观察。而需要注意的是其中token的结果:

"token": {

     "expires": "2012-09-27T13:41:15Z",

            "id": "d8444bd1ab8140ceb136d748eef86647",

            "tenant": {

                "description": null,

                "enabled": true,

                "id": "e8b18580e5b44cb79b10bd0f7a03bbdc",

                "name": "admin"

            }

        }

在以下的接口操作中,都需要从该token中获取它的id,以及其对应租户(tenant)的id。

协议:

http

示例:

curl -X POST -d '{"auth": {"tenantName": "admin", "passwordCredentials":{"username": "admin", "password": "pengchao"}}}' -H "Content-type: application/json" http://192.168.0.191:35357/v2.0/tokens | python -mjson.tool

 

2)容器列表

接口名称:

获取容器列表

接口定义:

curl -s http://localhost:8080/v1/AUTH_{Tenant_id} \

     -X GET \

     -H "X-Auth-Token: { Token_id}"

 

传入参数定义:

Tenant_id:租户id

Token_id:租户对应的token的id

返回结果:

 

协议:

http

示例:

curl -s http://192.168.0.191:8080/v1/AUTH_e8b18580e5b44cb79b10bd0f7a03bbdc -X GET -H "X-Auth-Token: 22ec6a37ed6b4b23a435e6b9050f21a4"

 

3)创建容器

接口名称:

创建容器

接口定义:

curl -s http://localhost:8080/v1/AUTH_{Tenant_id}/{Container_name} \

     -X PUT \

     -H "X-Auth-Token: { Token_id}"

 

传入参数定义:

Tenant_id:租户id

Container_name: 所创建的容器名称

Token_id:租户对应的token的id

返回结果:

如果创建成功,返回“201 Created”

协议:

http

示例:

curl -s http://192.168.0.191:8080/v1/AUTH_e8b18580e5b44cb79b10bd0f7a03bbdc -X GET -H "X-Auth-Token: 22ec6a37ed6b4b23a435e6b9050f21a4"

 

4)删除容器

接口名称:

删除容器

接口定义:

curl -s http://localhost:8080/v1/AUTH_{Tenant_id}/{Container_name} \

     -X DELETE \

     -H "X-Auth-Token: { Token_id}"

传入参数定义:

Tenant_id:租户id

Container_name: 所删除的容器名称

Token_id:租户对应的token的id

返回结果:

如果删除成功,什么也没返回。可以使用列出容器的api验证。

协议:

http

示例:

curl -s http://192.168.0.191:8080/v1/AUTH_e8b18580e5b44cb79b10bd0f7a03bbdc/my_container -X DELETE -H "X-Auth-Token: 22ec6a37ed6b4b23a435e6b9050f21a4"

 

5)列出对象

接口名称:

列出容器对象

接口定义:

curl -s http://localhost:8080/v1/AUTH_{Tenant_id}/{Container_name}[?parm=value] \

     -X GET \

     -H "X-Auth-Token: { Token_id}"

传入参数定义:

Tenant_id:租户id

Container_name: 容器名称

Token_id:租户对应的token的id

返回结果:

 

协议:

http

示例:

curl -s http192.168.0.191:8080/v1/AUTH_AUTH_e8b18580e5b44cb79b10bd0f7a03bbdc/my_container?format=json -X GET -H "X-Auth-Token: 22ec6a37ed6b4b23a435e6b9050f21a4" | python -mjson.tool

 

6)创建对象

接口名称:

创建对象

接口定义:

curl –X PUT -i \

    -H "X-Auth-Token: { Token_id}" \

    -T {obj_path} \

    http://localhost:8080/v1/AUTH_{Tenant_id}/{Container_name}/{obj_name}

传入参数定义:

Tenant_id:租户id

Obj_path:对象的完整路径

obj_name:对象名称

Container_name: 容器名称

Token_id:租户对应的token的id

返回结果:

<html>

<head>

  <title>201 Created</title>

</head>

<body>

  <h1>201 Created</h1>

  <br /><br />

</body>

</html>

协议:

http

示例:

curl -X PUT -i -H "X-Auth-Token: 22ec6a37ed6b4b23a435e6b9050f21a4" -T ufw.log  http://192.168.0.191:8080/v1/AUTH_e8b18580e5b44cb79b10bd0f7a03bbdc/my_container/ufw.log

 

7)删除对象

接口名称:

删除对象

接口定义:

curl -s http://localhost:8080/v1/AUTH_{Tenant_id}/{Container_id}/{object_name} \

     -X DELETE \

     -H "X-Auth-Token: { Token_id}"

传入参数定义:

Tenant_id:租户id

Obj_name:对象名称

Container_name: 所删除的容器名称

Token_id:租户对应的token的id

返回结果:

无返回

协议:

http

示例:

curl -s http://192.168.0.191:8080/v1/AUTH_e8b18580e5b44cb79b10bd0f7a03bbdc/my_container/history24 -X DELETE -H "X-Auth-Token: 22ec6a37ed6b4b23a435e6b9050f21a4"

 

3、实例:使用Python实现

3.1、前提条件

由于数据是存放在request中,为了方便,先构造request,使用python(urllib2)代码实现如下:

@param:server_api  根据需要定义的rest url

@param:autho_token 管理员token

@param:send_data 需要发送的数据

@param:method 定义的方法【GET、PUT、DELETE、POST】,默认是POST


def get_server_request(server_api=None, auth_token=None, send_data=None, method=None):


    if not server_api:


        return None


 


    server_request = urllib2.Request(server_api)


    if not server_request:


        return None


 


    server_request.add_header('Content-Type', 'application/json;charset=utf8')


    server_request.add_header('Accept', 'application/json')


    server_request.add_header('User-Agent', 'python-mikeyp')


    if auth_token:


        server_request.add_header('X-Auth-Token', auth_token)


    if send_data:


        server_request.add_data(send_data)


    if method:


        server_request.get_method = lambda: method


 


    return server_request


 

1)获取keystone认证,进行任何CURD操作,都需要从keystone中获取管理员的租户ID和管理员的token,用Python代码实现如下:

@param:user_name 管理员

@param:password 管理员密码

@param:tenant_name 管理员对应的租户名称


def get_keystone_token(user_name=None, password=None, tenant_name=None):


    server_api = get_Keystone_Endpoint() + 'tokens'


 


    send_data = None


    if user_name and password and tenant_name:


        send_data = {'auth': {'tenantName': tenant_name, 'passwordCredentials': \


                           {'username': user_name, 'password': password}}}


    if not send_data:


        return None


 


    server_request = get_server_request(server_api, None, json.dumps(send_data), None)


    if not server_request:


        return None


 


    try:


        server_response = urllib2.urlopen(server_request)


        receiver_data = server_response.read()


 


        if receiver_data:


            data = json.loads(receiver_data)


            token = data['access']['token']['id']


            tenant_id = data['access']['token']['tenant']['id']


            return (tenant_id, token)


        else:


            return None


    except Exception:


        return None


 

3.2、主要功能实现

以下的代码都能单独使用,实现的关键在于对request操作,实现如下:


server_response = urllib2.urlopen(server_request)


1)列出容器

           

'''列出swift容器列表

@param:

@type:

@return:"count","bytes","name"

@rtype:json

'''


def Container_list():


    server_api = None


    if tenant_id:


        server_api = untils.get_Swift_Endpoint() + tenant_id


    if not server_api:


        api_logger.error('Container_list server_api: %s.' % server_api)


        return untils.rest_result()


 


    try:


        server_request = untils.get_server_request(server_api, token)


        api_logger.info('Container_list server_request: %s.' % server_request)


        if not server_request:


            return untils.rest_result()


        server_response = urllib2.urlopen(server_request)


        api_logger.info('Container_list server_response: %s.' % server_response)


 


        receiver_data = server_response.read()


        if receiver_data:


            data = json.loads(receiver_data)


            con_list = []


            for d in data:


                con_list.append(d['name'])


            return con_list


        return untils.rest_result(True, False)


    except Exception:


        api_logger.critical('Container_list function caught an exception.')


        return untils.rest_result(False, True)


2)创建容器                          

'''创建一个容器

@param:

@type:

@return:"count","bytes","name"

@rtype:json

'''


def Container_create(name=None):


    server_api = None


    if tenant_id:


        server_api = untils.get_Swift_Endpoint() + tenant_id + "/" + name


    if not server_api:


        api_logger.error('Container_create server_api: %s.' % server_api)


        return untils.rest_result()


 


    try:


        server_request = untils.get_server_request(server_api, token, None, "PUT")


        api_logger.info('Container_create server_request: %s.' % server_request)


        if not server_request:


            return untils.rest_result()


        server_response = urllib2.urlopen(server_request)


        api_logger.info('Container_create server_response: %s.' % server_response)


 


        receiver_data = server_response.read()


        if receiver_data:


            data = json.dumps(receiver_data)


            return untils.rest_result(True, False, data)


        return untils.rest_result(True, False)


    except Exception:


        api_logger.critical('Container_create function caught an exception.')


        return untils.rest_result(False, True)


3)删除容器

'''删除一个容器

@param:

@type:

@return:"count","bytes","name"

@rtype:json

'''


def Container_delete(con_name=None):


    if con_name not in Container_list():


        return untils.rest_result()


    obj_list= Object_list(con_name)[0]


    if obj_list:


        api_logger.info("the contaniner is not empty!")


        for obj in obj_list:


            Object_delete(con_name,obj)


 


    server_api = None


    if tenant_id:


        server_api = untils.get_Swift_Endpoint() + tenant_id + "/" + con_name


    if not server_api:


        api_logger.error('Container_delete server_api: %s.' % server_api)


        return untils.rest_result()


 


    try:


        server_request = untils.get_server_request(server_api, token, None, "DELETE")


        api_logger.info('Container_delete server_request: %s.' % server_request)


        if not server_request:


            return untils.rest_result()


        server_response = urllib2.urlopen(server_request)


        api_logger.info('Container_delete server_response: %s.' % server_response)


 


        receiver_data = server_response.read()


        if receiver_data:


            data = json.dumps(receiver_data)


            return untils.rest_result(True, False, data)


        return untils.rest_result(True, False)


    except Exception:


        api_logger.critical('Container_delete function caught an exception.')


        return untils.rest_result(False, True)


4)列出对象

'''列出swift某容器的对象列表

@param_name:容器名称

@type:char

@return:"bytes","last_modified","hash","name","content_type"

@rtype:json

'''


def Object_list(container_name=None):


    server_api = None


    if tenant_id:


        server_api = untils.get_Swift_Endpoint() + tenant_id + "/" +_name


    if not server_api:


        api_logger.error('Object_list server_api: %s.' % server_api)


        return untils.rest_result()


 


    try:


        server_request = untils.get_server_request(server_api, token)


        api_logger.info('Object_list server_request: %s.' % server_request)


        if not server_request:


            return untils.rest_result()


        server_response = urllib2.urlopen(server_request)


        api_logger.info('Object_list server_response: %s.' % server_response)


 


        receiver_data = server_response.read()


        if receiver_data:


            data = json.loads(receiver_data)


            c_l = []


            for d in data:


                c_l.append(d['name'])


            return untils.rest_result(True, False, c_l)


        return untils.rest_result(True, False)


    except Exception:


        api_logger.critical('Object_list function caught an exception.')


        return untils.rest_result(False, True)


5)上传对象

'''在一个容器内创建、上传一个对象

@param_name:容器名称

@param file_name:对象名称

@type:char

@return:

@rtype:json

'''


def Object_upload(container_name=None, obj_path=None, obj_name=None):


    send_data = None


    obj_full_path = os.path.join(obj_path, obj_name)


    with open(obj_full_path) as obj:


        send_data = json.dumps(obj.read())


 


    server_api = None


    if tenant_id:


        server_api = untils.get_Swift_Endpoint() + tenant_id + "/" +_name + "/" + obj_name


    if not server_api:


        api_logger.error('Object_upload server_api: %s.' % server_api)


        return untils.rest_result()


 


    try:


        server_request = untils.get_server_request(server_api, token, send_data, 'PUT')


        api_logger.info('Object_upload server_request: %s.' % server_request)


        if not server_request:


            return untils.rest_result()


        server_response = urllib2.urlopen(server_request)


        api_logger.info('Object_upload server_response: %s.' % server_response)


 


        receiver_data = server_response.read().strip()


        if receiver_data:


            data = json.dumps(receiver_data)


            return untils.rest_result(True, False, data)


        return untils.rest_result(True, False)       


    except Exception:


        api_logger.critical('Object_upload function caught an exception.')


        return untils.rest_result(False, True)


6)下载对象

'''从容器下载一个对象

@param_name:容器名称

@param file_name:对象名称

@type:char

@return:

@rtype:json

'''


def Object_download(container_name=None, obj_path=None, obj_name=None):


    server_api = None


    if tenant_id:


        server_api = untils.get_Swift_Endpoint() + tenant_id + "/" +_name + "/" + obj_name


    if not server_api:


        api_logger.error('Object_download server_api: %s.' % server_api)


        return untils.rest_result()


 


    try:


        server_request = untils.get_server_request(server_api, token)


        api_logger.info('Object_download server_request: %s.' % server_request)


        if not server_request:


            return untils.rest_result()


        server_response = urllib2.urlopen(server_request)


        api_logger.info('Object_download server_response: %s.' % server_response)


 


        receiver_data = server_response.read()


        if receiver_data:


            obj_full_path = os.path.join(obj_path, obj_name)


            with open(obj_full_path, "wb") as code:


                code.write(receiver_data)


        return untils.rest_result(True, False)


    except Exception:


        api_logger.critical('Object_download function caught an exception.')


        return untils.rest_result(False, True)


7)删除对象

'''从容器删除一个对象

@param_name:容器名称

@param file_name:对象名称

@type:char

@return:

@rtype:json

'''


def Object_delete(container_name=None, obj_name=None):


    server_api = None


    if tenant_id:


        server_api = untils.get_Swift_Endpoint() + tenant_id + "/" +_name + "/" + obj_name


    if not server_api:


        api_logger.error('Object_delete server_api: %s.' % server_api)


        return untils.rest_result()


 


    try:


        server_request = untils.get_server_request(server_api, token, None, 'DELETE')


        api_logger.info('Object_delete server_request: %s.' % server_request)


        if not server_request:


            return untils.rest_result()


        server_response = urllib2.urlopen(server_request)


        api_logger.info('Object_delete server_response: %s.' % server_response)


 


        receiver_data = server_response.read()


        if receiver_data:


            data = json.dumps(receiver_data)


            return untils.rest_result(True, False, data)


        return untils.rest_result(True, False)


    except Exception:


        api_logger.critical('Object_delete function caught an exception.')


        return untils.rest_result(False, True)


https://www.xamrdz.com/lan/5xf1938456.html

相关文章: