• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

curl操作openstack swift

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

curl操作openstack swift

1、简介
在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。
首先介绍一下我们操作openstack swift API所需要的curl 命令参数。

-i 把HTTP的请求结果显示在标准输出设备上
-X(大写) 定义HTTP的操作类型(如GET、HEAD、PUT、POST、DELETE)
-H 请求描述一个http头部
-d 将数据传输到服务器,就像在网页游览器中登陆用户。

2、这里简单介绍一下swift 的身份认证系统,一种是openstack的keystone作为用户的身份与权限认证,另外一种是Swift自带的TempAuth作为用户的身份与权限认证。这两种身份认证的原理有所不同,本次实验采用更为科学的keystone身份认证系统。
openstack swift的API 接口:

更具openstack官网的操作来获取 项目范围 的相关信息和token:

[[email protected] ~]# curl -i -H "Content-Type:application/json" -d '{"auth":{"identity":{"methods":["password"],"password":{"user":{"name":"admin","domain":{"name":"demo"},"password":"2090bd7c719992a16e52"}}},"scope":{"project":{"name":"admin","domain":{"name":"demo"}}}}}' http://controller:5000/v3/auth/tokens

HTTP/1.1 201 Created
Date: Thu, 14 Mar 2019 20:45:34 GMT
Server: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5
X-Subject-Token: gAAAAABcir1urx28TBs4RhoauNxwDXoUF4oPuimMkeTtuAIM4b-yFNuw-Lp37c0pHEUJyXY7gD8Elo5_4p7h5johUr8woBYNFnQ4492IBlBKxJ9kw-EkHG_FfppxVV7-qaaJQNNNhJe0Qz4OTtn7Lvvy8N1WGlNeQqK4N928Tt1Ef0L2PAL6KFY
Vary: X-Auth-Token
x-openstack-request-id: req-53eb741f-133f-4905-9663-7f453a002d63
Content-Length: 4771
Content-Type: application/json

{"token": {"methods": ["password"], "roles": [{"id": "29e0dcd3a7794c038a676559bb61d124", "name": "admin"}], "expires_at": "2019-03-14T21:45:34.696614Z", "project": {"domain": {"id": "1c66cc15c3db4e7e8f882f2443adfa8d", "name": "demo"}, "id": "f9fd5e2aa54d4c8caa070b58507377c5", "name": "admin"}, "catalog": [{"endpoints": [{"region_id": "RegionOne", "url": "http://controller:8776/v2/f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "internal", "id": "a278bcd0c97b43c495b388147ea413e9"}, {"region_id": "RegionOne", "url": "http://controller:8776/v2/f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "public", "id": "ea34f0bb661341ec912f9ab30cce5b0c"}, {"region_id": "RegionOne", "url": "http://controller:8776/v2/f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "admin", "id": "fbf2aacbbb2b4997926d252420eb99b5"}], "type": "volumev2", "id": "039b94fb718f44579d0bd0b56726e97e", "name": "cinderv2"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:8080/v1/AUTH_f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "internal", "id": "3b749a52d96044f393ce3287ffc1ecc1"}, {"region_id": "RegionOne", "url": "http://controller:8080/v1/AUTH_f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "public", "id": "481173f4dd244aadaf08d9bd52e0c4e7"}, {"region_id": "RegionOne", "url": "http://controller:8080/v1", "region": "RegionOne", "interface": "admin", "id": "63fc13a2503642ab9066826984620832"}], "type": "object-store", "id": "1c8dab39dc474b238bd1bf71addfc8ae", "name": "swift"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:9696", "region": "RegionOne", "interface": "admin", "id": "00449945602645ad9c9568ab119da6a1"}, {"region_id": "RegionOne", "url": "http://controller:9696", "region": "RegionOne", "interface": "internal", "id": "5c07ad8a938e42ad92bb489bf3e10824"}, {"region_id": "RegionOne", "url": "http://controller:9696", "region": "RegionOne", "interface": "public", "id": "91c89eb0b1a44050849134d00bb4f3c6"}], "type": "network", "id": "5c8e5d3f0b344b288cfdb159ea15c53b", "name": "neutron"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:8774/v2.1/f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "public", "id": "69ce53fd7d0c47578aafa8a5cd2a4faf"}, {"region_id": "RegionOne", "url": "http://controller:8774/v2.1/f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "admin", "id": "83ec80ce733e4d66a998ab18b970463b"}, {"region_id": "RegionOne", "url": "http://controller:8774/v2.1/f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "internal", "id": "97e46d9342094937b0228261e4b89f8e"}], "type": "compute", "id": "65181754639f405699e14c23f28f80ab", "name": "nova"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:8776/v1/f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "admin", "id": "292ae9c7db844dcb9a53e1e7c61cd381"}, {"region_id": "RegionOne", "url": "http://controller:8776/v1/f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "internal", "id": "3c67855d84f445f6abab70760aa2438a"}, {"region_id": "RegionOne", "url": "http://controller:8776/v1/f9fd5e2aa54d4c8caa070b58507377c5", "region": "RegionOne", "interface": "public", "id": "81e9ebaae41842bb99e745eed012530e"}], "type": "volume", "id": "a0261e8d34fe4433a87a70a3c3499f74", "name": "cinder"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:9292", "region": "RegionOne", "interface": "public", "id": "53c8ddad40b347399401ec9751d1f786"}, {"region_id": "RegionOne", "url": "http://controller:9292", "region": "RegionOne", "interface": "admin", "id": "7e3f2e899afb4e55a7019daf52848f09"}, {"region_id": "RegionOne", "url": "http://controller:9292", "region": "RegionOne", "interface": "internal", "id": "f30ef9bac76c49e69da1ef564c849ccb"}], "type": "image", "id": "e407f7bcd172479383894ab3d939bfa2", "name": "glance"}, {"endpoints": [{"region_id": "RegionOne", "url": "http://controller:5000/v3", "region": "RegionOne", "interface": "public", "id": "0200604880974248ae69dfbaea2881eb"}, {"region_id": "RegionOne", "url": "http://controller:5000/v3", "region": "RegionOne", "interface": "internal", "id": "0d36f81d3c47412c9fcdd43bb5f921d5"}, {"region_id": "RegionOne", "url": "http://controller:35357/v3", "region": "RegionOne", "interface": "admin", "id": "f66ecda0003e4b8085ba2083829b8303"}], "type": "identity", "id": "f071367658f841259ba732939328cdd3", "name": "keystone"}], "user": {"domain": {"id": "1c66cc15c3db4e7e8f882f2443adfa8d", "name": "demo"}, "id": "8e45df8e7bf74dccac78ec3914d62d37", "name": "admin"}, "audit_ids": ["v3IHWUpgTrmqajYk4woIgQ"], "issued_at": "2019-03-14T20:45:34.000000Z"}}[[email protected] ~]# 

curl -i -H “Content-Type:application/json” -d ‘{“auth”:{“identity”:{“methods”:[“password”],“password”:{“user”:{“name”:“admin”,“domain”:{“name”:“demo”},“password”:“2090bd7c719992a16e52”}}},“scope”:{“project”:{“name”:“admin”,“domain”:{“name”:“demo”}}}}}’ http://controller:5000/v3/auth/tokens

关于这条命令很长,不过也很好记忆(因为它是json格式),我已经把上面需要更换为自定义的内容加粗

2、接下来我们来对swift进行相关的操作。
1)创建一个没有元数据的container(容器)

[[email protected] ~]# token=gAAAAABcir1urx28TBs4RhoauNxwDXoUF4oPuimMkeTtuAIM4b-yFNuw-Lp37c0pHEUJyXY7gD8Elo5_4p7h5johUr8woBYNFnQ4492IBlBKxJ9kw-EkHG_FfppxVV7-qaaJQNNNhJe0Qz4OTtn7Lvvy8N1WGlNeQqK4N928Tt1Ef0L2PAL6KFY
[[email protected] ~]# curl -i -H "X-Auth-Token:$token" -X PUT http://controller:8080/v1/AUTH_f9fd5e2aa54d4c8caa070b58507377c5/container2
HTTP/1.1 201 Created
Content-Length: 0
Content-Type: text/html; charset=UTF-8
X-Trans-Id: tx043e29ae73c2402787f21-005c8ac07f
Date: Thu, 14 Mar 2019 20:58:39 GMT


2)创建一个有元数据的的容器,规定容器上传单个文件的最大值为:10000字节(如果需要其他添加元数据,可以参照以下格式继续添加)。

[[email protected] ~]# curl -i -X PUT -H "X-Auth-Token:$token" -H "X-Container-Meta-Quota-Bytes:10000"  http://controller:8080/v1/AUTH_f9fd5e2aa54d4c8caa070b58507377c5/container3
HTTP/1.1 201 Created
Content-Length: 0
Content-Type: text/html; charset=UTF-8
X-Trans-Id: tx7fd9a58ace0d4cce9c668-005c8ac198
Date: Thu, 14 Mar 2019 21:03:20 GMT


3)查询容器。

[[email protected] ~]# curl -i -X GET -H "X-Auth-Token:$token" http://controller:8080/v1/AUTH_f9fd5e2aa54d4c8caa070b58507377c5
HTTP/1.1 200 OK
Content-Length: 22
X-Account-Object-Count: 0
X-Account-Storage-Policy-Policy-0-Bytes-Used: 0
X-Account-Storage-Policy-Policy-0-Container-Count: 2
X-Timestamp: 1550262902.05009
X-Account-Storage-Policy-Policy-0-Object-Count: 0
X-Account-Bytes-Used: 0
X-Account-Container-Count: 2
Content-Type: text/plain; charset=utf-8
Accept-Ranges: bytes
x-account-project-domain-id: 1c66cc15c3db4e7e8f882f2443adfa8d
X-Trans-Id: tx1d386a2ffbe34545a4fe6-005c8ac2d3
Date: Thu, 14 Mar 2019 21:08:35 GMT

container2
container3


4)显示容器元数据信息;

[[email protected] ~]# curl -i  -X HEAD -H "X-Auth-Token:$token" http://controller:8080/v1/AUTH_f9fd5e2aa54d4c8caa070b58507377c5/container3
HTTP/1.1 204 No Content
Content-Length: 0
X-Container-Object-Count: 0
Accept-Ranges: bytes
X-Storage-Policy: Policy-0
X-Container-Bytes-Used: 0
X-Timestamp: 1552597401.08738
Content-Type: text/plain; charset=utf-8
X-Container-Meta-Quota-Bytes: 10000
X-Trans-Id: tx9244169feff5436fbdb63-005c8ac38b
Date: Thu, 14 Mar 2019 21:11:39 GMT


5)上传文件

上传网卡文件ifcfg-enp8s0 到container2容器中。
[[email protected] ~]# curl -i http://controller:8080/v1/AUTH_f9fd5e2aa54d4c8caa070b58507377c5/container2/ifcfg-enp8s0   -X PUT -d "Hello" -H "Content-Type: text/html; charset=UTF-8" -H "X-Auth-Token:`openstack token issue |awk -F '|' '/ id/ {print $3}'`"                        
HTTP/1.1 201 Created
Last-Modified: Thu, 14 Mar 2019 21:33:22 GMT
Content-Length: 0
Etag: 8b1a9953c4611296a827abf8c47804d7
Content-Type: text/html; charset=UTF-8
X-Trans-Id: txb165a9270ed94202be274-005c8ac8a1
Date: Thu, 14 Mar 2019 21:33:21 GMT

[[email protected] ~]# curl -i  -X GET -H "X-Auth-Token:$token" http://controller:8080/v1/AUTH_f9fd5e2aa54d4c8caa070b58507377c5/container2       //查询
HTTP/1.1 200 OK
Content-Length: 37
X-Container-Object-Count: 2
Accept-Ranges: bytes
X-Storage-Policy: Policy-0
X-Container-Bytes-Used: 8
X-Timestamp: 1552597119.85380
Content-Type: text/plain; charset=utf-8
X-Trans-Id: tx1a4c6814a1cd4bdcb2b4c-005c8ac932
Date: Thu, 14 Mar 2019 21:35:47 GMT

container3/ifcfg-enp8s0
ifcfg-enp8s0



5)删除容器和对象(由于这两个类似,只演示一个)

[[email protected] ~]# curl -i  -X DELETE  -H "X-Auth-Token:`openstack token issue |awk -F '|' '/ id/ {print $3}'`" http://controller:8080/v1/AUTH_f9fd5e2aa54d4c8caa070b58507377c5/container3
HTTP/1.1 204 No Content
Content-Length: 0
Content-Type: text/html; charset=UTF-8
X-Trans-Id: tx6bb0bacd1c5046bfa0227-005c8acb12
Date: Thu, 14 Mar 2019 21:43:46 GMT

如果容器不存在,则响应为:

HTTP/1.1 404 Not Found
Content-Length: 70
Content-Type: text/html; charset=UTF-8
X-Trans-Id: tx4d728126b17b43b598bf7-0052d81e34
X-Openstack-Request-Id: tx4d728126b17b43b598bf7-0052d81e34
Date: Thu, 16 Jan 2014 18:00:20 GMT

如果容器存在且删除成功,则响应为:

HTTP/1.1 204 No Content
Content-Length: 0
Content-Type: text/html; charset=UTF-8
X-Trans-Id: txf76c375ebece4df19c84c-0052d81f14
X-Openstack-Request-Id: txf76c375ebece4df19c84c-0052d81f14
Date: Thu, 16 Jan 2014 18:04:04 GMT

如果容器存在但不为空,则响应为:

HTTP/1.1 409 Conflict
Content-Length: 95
Content-Type: text/html; charset=UTF-8
X-Trans-Id: tx7782dc6a97b94a46956b5-0052d81f6b
X-Openstack-Request-Id: tx7782dc6a97b94a46956b5-0052d81f6b
Date: Thu, 16 Jan 2014 18:05:31 GMT
<html>
<h1>Conflict
</h1>
<p>There was a conflict when trying to complete your request.
</p>
</html>

正常响应代码:204

错误响应代码:404,409


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode704.二分查找|BinarySearch发布时间:2022-07-13
下一篇:
swift学习笔记1——基础部分发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap