cURL operation Openstack object store ReST API details

  • 2020-05-27 07:39:32
  • OfStack

Due to the recent work needs to use Openstack, cURL is used to operate ReST API stored by Openstack object. I found a lot of data to complete this, so I will record it here.

Use Openstack and cURL to manipulate ReST API stored by Openstack objects

cURL is a tool for transferring files and data using the URL syntax. It supports many protocols, such as HTTP, FTP, TELNET, and so on. This article focuses on how you can use the tool and the HTTP protocol to interact with the swift service. cURL allows you to send and receive HTTP requests and responses from the command line or shell scripts. This makes it possible to work directly with ReST's API without the need for another customer service side, APIs. For this article, we need to use the following cURL command-line options:


-X METHOD  describe HTTP Request method of (HEAD, GET Etc. ) 

-D Dump  will HTTP Response head to stdout.

-H HEADER  describe 1 In the request HTTP\HTTPS The head .

-v  The process of using the operation is more detailed 

1. Certification

In order to use the ReST API, you will first need to obtain a authorization token, which will need to be passed in for each request using the X-Auth-Token header. The following example demonstrates how to use cURL to obtain the authorization token and the URL of the storage system.

In order to use ReST's API, we first need to obtain an authentication token to pass to each request that USES the X-Auth-Token header. The following example shows how to use cURL to obtain an authentication token and URL for a storage system.

Exp1: obtain 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 are as follows:


sting' http://127.0.0.1:8080/auth/v1.0
HTTP/1.1 200 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, 07 Oct 2011 07:45:58 GMT

You can also use the -v option for 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 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 200 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, 07 Oct 2011 07:48:30 GMT
< 
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

Stores the URL and the authentication token as the response header. After certification, you can use HEAD cURL to perform on the storage service, GET, DELETE, POST and PUT request.

2. Determine storage usage

An HEAD request can be sent to the storage service to determine how much data you have stored on your system and how many containers you are using. Use the -X switch to describe the correct HTTP method and -D to output the HTTP response header to the terminal (stdout).

Exp2: check the storage usage of the account


 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, 07 Oct 2011 08:04:38 GMT

The HTTP request must contain a header describing the authentication token. The HTTP header in the response shows the number of containers in the storage account and the total number of bytes stored for 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 the PUT request to create a container; cURL can also be used for this purpose.

Exp3: create a container of photos

curl -X PUT -D - -H "X-Auth-Token:AUTH_tkf828cc87bb9348168a52619b1f7e3928" http://127.0.0.1:8080/v1/AUTH_test/photos

The results are as follows:


HTTP/1.1 201 Created
Content-Length: 18
Content-Type: text/html; charset=UTF-8
Date: Fri, 07 Oct 2011 08:18:44 GMT

The HTTP status code returning 201 (Created) indicates that the container was successfully created.

4. Upload 1 storage object

After creating a container, you can upload a local file. For this example, we upload a photo of Lin chi-ling MM. -T switch describes the full path to the uploaded local file.

Exp4: upload objects


curl -X PUT -T /home/swift/ download /lzl.jpg -D - \
 -H "Content-Type: image/jpeg" \
 -H "X-Auth-Token: AUTH_tkf828cc87bb9348168a52619b1f7e3928" \
 -H "X-Object-Meta-lzl: 1 A beautiful picture of Lin chi-ling " \
 http://127.0.0.1:8080/v1/AUTH_test/photos/lzl.jpg

The results are as follows:


HTTP/1.1 201 Created
Content-Length: 118
Content-Type: text/html; charset=UTF-8
Etag: 870563216b9f54942fc09d574aa3e2bd
Last-Modified: Fri, 07 Oct 2011 08:39:49 GMT
Date: Fri, 07 Oct 2011 08:39:51 GMT

5. Other cURL commands

You can use the cURL tool to emit any ReST method defined for OpenStack object storage. For example, you can use cURL to send POST and DELETE requests even if we do not provide the relevant examples. For example, download the photo you just uploaded:

curl -X GET -H "X-Auth-Token: AUTH_tkf828cc87bb9348168a52619b1f7e3928" http://127.0.0.1:8080/v1/AUTH_test/photos/lzl.jpg > td_lzl.jpg

Note: 1. Each time you call curl to perform an operation, a separate TCP/IP and SSL connection (https) is created and discarded. However, APIs, which USES the language, is designed to reuse these connections between operations, thus providing better refinement. We recommend the use of APIs in one language in your product applications and limit curl to quick and easy testing/diagnostics only.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: