Due to the recent work need to use OpenStack, use Curl Operation OpenStack Object storage Rest API, this is I found a lot of information, here, recorded.
Using OpenStack, use curl to manipulate the rest API for OpenStack object storage
CURL is a tool that uses URL syntax to transfer files and data, and supports many protocols, such as HTTP, FTP, Telnet, and so on. This article is mainly about how to interact with the swift service using this tool and the HTTP protocol. Curl allows you to send and receive HTTP requests and responses from the command line or from a shell script. This makes it possible to work directly with rest APIs without the need for other client-side APIs. In this article, we need to use the following Curl command-line options:
The-X method describes the HTTP request methods (Head, get, and so on)-
D Dump to the HTTP response header to the stdout.
The-H header describes a Http\https header in the request.
-V Use the process of the operation in more detail
1. Certification
In order to use the ReST API, you'll need to obtain a authorization token, which'll need to is passed in for EAC H request using the X-auth-token header. The following example demonstrates the obtain authorization and the URL of the token system.
In order to use the rest API, we first need to obtain an authentication token to pass to each request using the X-auth-token header. The following example shows how to use curl to obtain the authentication token and the URL of the storage system.
EXP1: Getting X-storage-url and X-auth-token
Curl-d--H ' x-storage-user:test:tester '-h ' x-storage-pass:testing ' http://127.0.0.1:8080/auth/v1.0
The results of the operation are as follows:
Sting ' http://127.0.0.1:8080/auth/v1.0
http/1.1 OK
x-storage-url:http://127.0.0.1:8080/v1/auth_test
x-storage-token:auth_tkf828cc87bb9348168a52619b1f7e3928
x-auth-token:auth_tkf828cc87bb9348168a52619b1f7e3928
content-length:0
date:fri, modified Oct 07:45:58 GMT
You can also use the-V option to obtain more detailed information:
Curl-v-H ' x-storage-user:test:tester '-h ' x-storage-pass:testing ' http://127.0.0.1:8080/auth/v1.0
The results of the operation are as follows:
* About to connect () to 127.0.0.1 port 8080 (#0)
* trying 127.0.0.1 ... connected * connected to
127.0.0.1 (127 .0.0.1) port 8080 (#0)
> get/auth/v1.0 http/1.1
> user-agent:curl/7.21.3 (I686-PC-LINUX-GNU) libcurl/ 7.21.3 openssl/0.9.8o zlib/1.2.3.4 libidn/1.18
> host:127.0.0.1:8080
> Accept: */*
> X-storage-user:test:tester
> X-storage-pass:testing
>
< http/1.1 OK
< X-storage-url:http://127.0.0.1:8080/v1/auth_test
< X-storage-token:auth_ tkf828cc87bb9348168a52619b1f7e3928
< x-auth-token:auth_tkf828cc87bb9348168a52619b1f7e3928
< Content-length:0
< Date:fri, modified Oct 07:48:30 GMT
<
* Connection #0 to host 127.0.0.1 left Inta CT
* Closing connection #0
The stored URL and authentication token are returned as the response head. After authentication, you can use curl to perform head,get,delete,post and put requests on the storage service.
2. Determine the use of storage
A head request can be sent to a storage service to determine how much data you have stored in the system and the number of containers you use. Use the-x switch to describe the correct HTTP method and-D to output the HTTP response head to the terminal (stdout).
EXP2: Query account for storage usage
Curl-x head-d-
h "x-auth-token:auth_tkf828cc87bb9348168a52619b1f7e3928" \
http://127.0.0.1:8080/v1/AUTH_test
The output is as follows:
http/1.1 204 No Content
x-account-object-count:3
x-account-bytes-used:92983
x-account-container-count : 4
accept-ranges:bytes
content-length:0
date:fri, modified Oct 08:04:38 GMT
The HTTP request must contain a header to describe the authentication token. The HTTP header in the response displays the number of containers in the storage account and the total number of bytes stored by the entire account.
3. Create a storage container
Before uploading any data to the OpenStack object store, you must create a storage container. You can use a put request to create a container; Curl can also be used for that purpose.
EXP3: Create a photos container
Curl-x put-d---H "x-auth-token:auth_tkf828cc87bb9348168a52619b1f7e3928" Http://127.0.0.1:8080/v1/AUTH_test/photos
The results of the operation are as follows:
http/1.1 201 Created
content-length:18
content-type:text/html; charset=utf-8
Date:fri, Modified OCT 2011 08:18:44 GMT
Returns the HTTP status code for 201 (Created) indicating that the container was successfully created.
4. Upload a Storage object
After you create a container, you can upload a local file. For this example, we upload a picture of Lin Ling mm. The-t switch describes the full path to the uploaded local file.
EXP4: Uploading objects
Curl-x put-t/home/swift/Download/lzl.jpg-d-
h "content-type:image/jpeg" \-
H "x-auth-token:auth_tkf828cc87bb93 48168a52619b1f7e3928 "\
h" X-object-meta-lzl: A picture of the beauty of Lin Chi-ling "\
yun_qi_img/lzl.jpg
The results are as follows:
http/1.1 201 Created
content-length:118
content-type:text/html charset=utf-8
: 870563216B9F54942FC09D574AA3E2BD
Last-modified:fri, Modified Oct 08:39:49 GMT
Date:fri, Modified Oct 08:39:51 GMT
5. Other Curl Orders
You can use the Curl tool to emit any defined rest methods for OpenStack object storage. For example, you can use curl to send post and delete requests even if we do not provide relevant examples. For example, download the photo you just uploaded:
curl-x get-h "x-auth-token:auth_tkf828cc87bb9348168a52619b1f7e3928" yun_qi_img/lzl.jpg >td_lzl.jpg
Note: Generally, each time you call curl to perform an operation, the system creates a separate TCP/IP and SSL connection (HTTPS) and discards it. However, the use of language APIs is designed to reuse these connections between operations, thus providing a better tender. We recommend that you use a language-enabled APIs in your product application and limit curl to fast and simple testing/diagnostics.
Thank you for reading, I hope to help you, thank you for your support for this site!