分享

使用Curl操作OpenStack Swift

xioaxu790 2014-10-31 09:31:04 发表于 实践型 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 19998
问题导读

1、如何理解什么是Account?
2、怎样列出 Contailner呢?
3、如何设置 Object Metadata?





提示:以下操作均是使用的 swift tempauth认证机制。

获取 Token
  1. curl -k -v -H 'X-Storage-User: admin:admin' -H 'X-Storage-Pass: admin' http://192.168.30.150:8080/auth/v1.0
复制代码



如果正确,将会返回以下类似信息:
  1. * About to connect() to 192.168.30.150 port 8080 (#0)
  2. *   Trying 192.168.30.150... connected
  3. > GET /auth/v1.0 HTTP/1.1
  4. > User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
  5. > Host: 192.168.30.150:8080
  6. > Accept: */*
  7. > X-Storage-User: admin:admin
  8. > X-Storage-Pass: admin
  9. >
  10. < HTTP/1.1 200 OK
  11. < X-Storage-Url: http://192.168.30.150:8080/v1/AUTH_admin
  12. < X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5
  13. < Content-Type: text/html; charset=UTF-8
  14. < X-Storage-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5
  15. < Content-Length: 0
  16. < Date: Tue, 15 Oct 2013 01:49:59 GMT
  17. <
  18. * Connection #0 to host 192.168.30.150 left intact
  19. * Closing connection #0
复制代码



Account操作
  1. curl -k -v -X HEAD -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin
复制代码


如果正确,将会返回以下类似信息:
  1. * About to connect() to 192.168.30.150 port 8080 (#0)
  2. *   Trying 192.168.30.150... connected
  3. > HEAD /v1/AUTH_admin HTTP/1.1
  4. > User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
  5. > Host: 192.168.30.150:8080
  6. > Accept: */*
  7. > X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5
  8. >
  9. < HTTP/1.1 204 No Content
  10. < Content-Length: 0
  11. < Accept-Ranges: bytes
  12. < X-Timestamp: 1381806617.24083
  13. < X-Account-Bytes-Used: 0
  14. < X-Account-Container-Count: 1
  15. < Content-Type: text/plain; charset=utf-8
  16. < X-Account-Object-Count: 0
  17. < Date: Tue, 15 Oct 2013 05:17:23 GMT
  18. <
  19. * Connection #0 to host 192.168.30.150 left intact
  20. * Closing connection #0
复制代码



Container操作

列出 Contailner
  1. curl -i -X GET -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin
复制代码



如果正确,将会返回以下类似信息:
  1. HTTP/1.1 200 OK
  2. Content-Length: 5
  3. Accept-Ranges: bytes
  4. X-Timestamp: 1381806617.24083
  5. X-Account-Bytes-Used: 0
  6. X-Account-Container-Count: 1
  7. Content-Type: text/plain; charset=utf-8
  8. X-Account-Object-Count: 0
  9. Date: Tue, 15 Oct 2013 05:20:11 GMT
  10. test
复制代码



最后一行的test就是查询出来的内容。

创建 Container
  1. curl -i -X PUT -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin/myfile
复制代码



如果正确,将会返回以下类似信息:
  1. HTTP/1.1 201 Created
  2. Content-Length: 0
  3. Content-Type: text/html; charset=UTF-8
  4. Date: Tue, 15 Oct 2013 05:22:01 GMT
复制代码



我们再来查询一次看是否成功:
  1. curl -i -X GET -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin
复制代码



如果正确,将会返回以下类似信息:
  1. HTTP/1.1 200 OK
  2. Content-Length: 12
  3. Accept-Ranges: bytes
  4. X-Timestamp: 1381806617.24083
  5. X-Account-Bytes-Used: 0
  6. X-Account-Container-Count: 2
  7. Content-Type: text/plain; charset=utf-8
  8. X-Account-Object-Count: 0
  9. Date: Tue, 15 Oct 2013 05:23:18 GMT
  10. myfile
  11. test
复制代码



只列出部分 Container
很多时候 Container 会有很多个,Swift 默认会列出前10000个,但如果我们只看最前面几个,该怎么办? 以下示例只显示最前面一个 Container
  1. curl -i -X GET -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin?limit=1
复制代码



结果:
  1. HTTP/1.1 200 OK
  2. Content-Length: 7
  3. Accept-Ranges: bytes
  4. X-Timestamp: 1381806617.24083
  5. X-Account-Bytes-Used: 0
  6. X-Account-Container-Count: 2
  7. Content-Type: text/plain; charset=utf-8
  8. X-Account-Object-Count: 0
  9. Date: Tue, 15 Oct 2013 05:24:36 GMT
复制代码



那如果要列出最后几个 Container 又怎么办呢? 加一个 marker即可,以下示例列出 myfile之后的一个Container
  1. curl -i -X GET -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin?marker=myfile\&limit=1
复制代码



如果正确,将会返回以下类似信息:
  1. HTTP/1.1 200 OK
  2. Content-Length: 5
  3. Accept-Ranges: bytes
  4. X-Timestamp: 1381806617.24083
  5. X-Account-Bytes-Used: 0
  6. X-Account-Container-Count: 2
  7. Content-Type: text/plain; charset=utf-8
  8. X-Account-Object-Count: 0
  9. Date: Tue, 15 Oct 2013 05:28:53 GMT
复制代码



格式化输出 Container
  1. curl -i -X GET -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin?format=json
复制代码



如果正确,将会返回以下类似信息:
  1. HTTP/1.1 200 OK
  2. Content-Length: 86
  3. Accept-Ranges: bytes
  4. X-Timestamp: 1381806617.24083
  5. X-Account-Bytes-Used: 0
  6. X-Account-Container-Count: 2
  7. Content-Type: application/json; charset=utf-8
  8. X-Account-Object-Count: 0
  9. Date: Tue, 15 Oct 2013 05:29:58 GMT
  10. [{"count": 0, "bytes": 0, "name": "myfile"}, {"count": 0, "bytes": 0, "name": "test"}]
复制代码



除了JSON格式,还可以格式化XML,只需要将json改成xml 即可。

查看 Container metadata
  1. curl -i -X HEAD -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin/test
复制代码



如果正确,将会返回以下类似信息:
  1. HTTP/1.1 204 No Content
  2. Content-Length: 0
  3. X-Container-Object-Count: 0
  4. Accept-Ranges: bytes
  5. X-Timestamp: 1381806903.70007
  6. X-Container-Bytes-Used: 0
  7. Content-Type: text/plain; charset=utf-8
  8. Date: Tue, 15 Oct 2013 05:32:06 GMT
复制代码



删除 Container
  1. curl -i -X DELETE -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin/mytest
复制代码



Object操作

创建 Object
  1. curl -k -i -X PUT -T "apache-tomcat-6.0.36.tgz" -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin/myfile/apache-tomcat-6.0.36.tgz
复制代码



如果正确,将会返回以下类似信息:
  1. HTTP/1.1 100 Continue
  2. HTTP/1.1 201 Created
  3. Last-Modified: Tue, 15 Oct 2013 05:39:07 GMT
  4. Content-Length: 0
  5. Etag: 3dde098fd0b3a08d3f2867e4a95591ba
  6. Content-Type: text/html; charset=UTF-8
  7. Date: Tue, 15 Oct 2013 05:39:08 GMT
复制代码



列出 Object
  1. curl -k -i -X GET -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin/myfile
复制代码



如果正确,将会返回以下类似信息:
  1. HTTP/1.1 200 OK
  2. Content-Length: 25
  3. X-Container-Object-Count: 1
  4. Accept-Ranges: bytes
  5. X-Timestamp: 1381814521.71796
  6. X-Container-Bytes-Used: 6780936
  7. Content-Type: text/plain; charset=utf-8
  8. Date: Tue, 15 Oct 2013 05:40:43 GMT
  9. apache-tomcat-6.0.36.tgz
复制代码



另外 Object和 Container一样可以通过加参数来限制查询,具体示例可参考 Container操作。

下载 Object
  1. curl -k -X GET -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin/myfile/apache-tomcat-6.0.36.tgz > apache-tomcat-6.0.36.tgz
复制代码



如果正确,将会返回以下类似信息:
  1.   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
  2.                                  Dload  Upload   Total   Spent    Left  Speed
  3. 100 6622k  100 6622k    0     0  32.8M      0 --:--:-- --:--:-- --:--:-- 33.1M
  4. Copy Object
  5. curl -k -i -X PUT -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' \
  6. -H 'X-Copy-From: /myfile/apache-tomcat-6.0.36.tgz' \
  7. -H 'Content-Length:0' http://192.168.30.150:8080/v1/AUTH_admin/test/apache-tomcat-6.0.36.tgz
复制代码



将/myfile/apache-tomcat-6.0.36.tgz 拷贝到 /test/apache-tomcat-6.0.36.tgz,如果正确,将会返回以下类似信息:
  1. HTTP/1.1 201 Created
  2. Content-Length: 0
  3. X-Copied-From-Last-Modified: Tue, 15 Oct 2013 05:39:07 GMT
  4. X-Copied-From: myfile/apache-tomcat-6.0.36.tgz
  5. Last-Modified: Tue, 15 Oct 2013 05:47:52 GMT
  6. Etag: 3dde098fd0b3a08d3f2867e4a95591ba
  7. Content-Type: text/html; charset=UTF-8
  8. Date: Tue, 15 Oct 2013 05:47:52 GMT
复制代码



删除 Object
  1. curl -k -i -X DELETE -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' http://192.168.30.150:8080/v1/AUTH_admin/test/apache-tomcat-6.0.36.tgz
复制代码



如果正确,将会返回以下类似信息:
  1. HTTP/1.1 204 No Content
  2. Content-Length: 0
  3. Content-Type: text/html; charset=UTF-8
  4. Date: Tue, 15 Oct 2013 05:50:49 GMT
复制代码



通过之前的GET就能验证是否成功删除,此处略过。

设置 Object Metadata
  1. curl -k -i -X POST -H 'X-Auth-Token: AUTH_tk8a85916d63b14c568a4633b7920623c5' \
  2. -H 'X-Object-Meta-Breed: apache tomcat 6.0.36' \
  3. http://192.168.30.150:8080/v1/AUTH_admin/myfile/apache-tomcat-6.0.36.tgz
复制代码



如果正确,将会返回以下类似信息:
  1. HTTP/1.1 202 Accepted
  2. Content-Length: 76
  3. Content-Type: text/html; charset=UTF-8
  4. Date: Tue, 15 Oct 2013 05:54:09 GMT
复制代码



通过之前的HEAD,就能查看到刚才添加的元数据
  1. HTTP/1.1 200 OK
  2. Content-Length: 6780936
  3. X-Object-Meta-Breed: apache tomcat 6.0.36
  4. Accept-Ranges: bytes
  5. Last-Modified: Tue, 15 Oct 2013 05:54:08 GMT
  6. Etag: 3dde098fd0b3a08d3f2867e4a95591ba
  7. X-Timestamp: 1381816448.74507
  8. Content-Type: application/x-tar
  9. Date: Tue, 15 Oct 2013 05:55:53 GMT
复制代码





没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条