swift API的使用
最近楼主要使用swift的API接口,楼主有一个习惯,不管开发需要用到什么知识,都喜欢看官方文档,虽然大部分是英文,但是用起来还是感觉可靠,不过对于openstack给的swift API接口,可叫我吃了不少苦,所以写下这篇文章希望给有同样困惑的朋友帮助。
openstack官网有专门的开发者文档介绍如何使用curl操作swift(http://docs.openstack.org/api/openstack-object-storage/1.0/content/),虽然有部分接口有问题,不过大部分写的比较详细还是值得借鉴的。
1.curl的使用
首先,我们需要知道swift提供的标准API都是通过curl工具完成的,比如说PUT,GET,POST等等。
对于curl的使用这里简单说下,具体详细情况,可以通过man工具或者百度和谷歌自己丰富自己。
curl基本命令:
-H <line> 自定义头信息传递给服务器
-i 输出时bao阔protocol头信息
-k 允许不使用证书到SSL站点
-v 显示详细信息
-X<command> 指定命令
-d<data> HTTP POST方式传送数据
2.使用curl操作switf接口
由于楼主搭建的是keystone的主机所以通过以下命令获取token:
$curl -d '{"auth":{"tenantName": "DA","passwordCredentials":{"username": "system","password": "123456"}}}' -H "Content-type:application/json" http://192.168.120.104:5000/v2.0/tokens |python -m json.tool
tenantName,username,password都是之前自己已经设置好的直接替换就可以。
获得的结果如下:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1377 100 1282 100 95 9382 695 --:--:-- --:--:-- --:--:-- 9426
{
"access": {
"Metadata": {
"is_admin": 0,
"roles": [
"9fe2ff9ee4384b1894a90878d3e92bab",
"d10801a102c141e585f80d75692faeca",
"be6b8f0d5ea049618ac29e109a320271"
]
},
"serviceCatalog": [
{
"endpoints": [
{
"adminURL": "http://192.168.120.104:8080/v1",
"id": "3d5f39395faa4b368d8dafee2ef6338c",
"internalURL": "http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec",
"publicURL": "http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "swift",
"type": "object-store"
},
{
"endpoints": [
{
"adminURL": "http://192.168.120.104:35357/v2.0",
"id": "1d8c83a975ac46afb6f950a152029a1e",
"internalURL": "http://192.168.120.104:5000/v2.0",
"publicURL": "http://192.168.120.104:5000/v2.0",
"region": "RegionOne"
}
],
"name": "keystone",
"type": "identity"
}
],
"token": {
"expires": "2016-02-02T10:47:22Z",
"id": "f2510429420f443e8815bb06012d070d",
"issued_at": "2016-02-02T09:47:22.974729",
"tenant": {
"description": "dam tenant",
"enabled": true,
"id": "1cfaab6763b04ebca93e8870436cd8ec",
"name": "da"
}
},
"user": {
"id": "76ea45ed8af74fbd9538e740dcbcd97a",
"name": "system",
"roles": [
{
"name": "_member_"
},
{
"name": "DA System Managers"
},
{
"name": "admin"
}
],
"roles_links": [],
"username": "system"
}
}
}
Dload Upload Total Spent Left Speed
100 1377 100 1282 100 95 9382 695 --:--:-- --:--:-- --:--:-- 9426
{
"access": {
"Metadata": {
"is_admin": 0,
"roles": [
"9fe2ff9ee4384b1894a90878d3e92bab",
"d10801a102c141e585f80d75692faeca",
"be6b8f0d5ea049618ac29e109a320271"
]
},
"serviceCatalog": [
{
"endpoints": [
{
"adminURL": "http://192.168.120.104:8080/v1",
"id": "3d5f39395faa4b368d8dafee2ef6338c",
"internalURL": "http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec",
"publicURL": "http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "swift",
"type": "object-store"
},
{
"endpoints": [
{
"adminURL": "http://192.168.120.104:35357/v2.0",
"id": "1d8c83a975ac46afb6f950a152029a1e",
"internalURL": "http://192.168.120.104:5000/v2.0",
"publicURL": "http://192.168.120.104:5000/v2.0",
"region": "RegionOne"
}
],
"name": "keystone",
"type": "identity"
}
],
"token": {
"expires": "2016-02-02T10:47:22Z",
"id": "f2510429420f443e8815bb06012d070d",
"issued_at": "2016-02-02T09:47:22.974729",
"tenant": {
"description": "dam tenant",
"enabled": true,
"id": "1cfaab6763b04ebca93e8870436cd8ec",
"name": "da"
}
},
"user": {
"id": "76ea45ed8af74fbd9538e740dcbcd97a",
"name": "system",
"roles": [
{
"name": "_member_"
},
{
"name": "DA System Managers"
},
{
"name": "admin"
}
],
"roles_links": [],
"username": "system"
}
}
}
注意红色的俩行接下我们使用API所用到的权限。
token:
f2510429420f443e8815bb06012d070d
publicURL:
f2510429420f443e8815bb06012d070d
3.account操作
查看当前的存储信息:
$curl -k -i -X GET -H 'X-Auth-Token:f2510429420f443e8815bb06012d070d' http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec
获取结果如下:
创建container
$curl -i -X PUT -H 'X-Auth-Token:f2510429420f443e8815bb06012d070d' http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec/lyw_test_vol
现在用上边的命令可以看到俩个container了。
格式化输出获取的container信息
$curl -k -i -X GET -H 'X-Auth-Token:f2510429420f443e8815bb06012d070d' http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec?format=json 注:json格式熟悉python的应该都清楚。
查看container的元数据:
写到这里由于swift的token是时时更新的,所以楼主必须用新的token值才能获取到正确结果,若以大家要明白。
$curl -k -i -X HEAD -H 'X-Auth-Token:0139369a54634743ab4526a3b48ed3aa' http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec/vol0000
删除container:
$curl -k -i -X DELETE -H 'X-Auth-Token:0139369a54634743ab4526a3b48ed3aa' http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec/vol0000
4.object操作
上传一个object到container中,switf先把这个object传到缓存中然后才传到相应的位置,在缓存中最小单位是object我们是看不到这个东西的,不过传到continer中最小单位就是file了我们是可以看到这个东西的,可以完整的put和get获取其中的数据和元数据。
创建object
$
curl -k -i -X PUT -T "hello.txt" -H 'X-Auth-Token:f2510429420f443e8815bb06012d070d' http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec/vol0000/hello.txt
下载刚才上传的object
$curl -k -i -X GET -H 'X-Auth-Token:0139369a54634743ab4526a3b48ed3aa'/ http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec/vol0000/hello.txt>1
删除object
$curl -k -i -X DELETE -H 'X-Auth-Token:0139369a54634743ab4526a3b48ed3aa'/ http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec/vol0000/hello.txt
objectMetadata操作
$curl -k -i -X POST -H 'X-Auth-Token:0139369a54634743ab4526a3b48ed3aa' -H 'X-Object-Meta-Breed:helloinfo' http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec/vol0000/hello.txt
读取元数据信息:
$curl -k -i -X HEAD -H 'X-Auth-Token:0139369a54634743ab4526a3b48ed3aa' http://192.168.120.104:8080/v1/AUTH_1cfaab6763b04ebca93e8870436cd8ec/vol0000/hello.txt