Swift/使い方

インストール設定ファイルの作成起動用スクリプトの作成が終わったら実際に動かしてみましょう。

サーバの起動

スクリプトを使用して、各種サーバを起動させます。
まずはRingを構築します。

# remakerings

テストを行ないます。

# cd ~/swift/trunk; ./.unittests
......................................................................
======================================================================
FAIL: test_whataremyips (test.unit.common.test_utils.TestUtils)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/nanodayo/swift/trunk/test/unit/common/test_utils.py", line 226, in test_whataremyips
    self.assert_(len(myips) > 1)
AssertionError

Name                         Stmts   Exec  Cover   Missing
----------------------------------------------------------
swift                            0      0   100%
swift.account                    0      0   100%

(中略)
 
----------------------------------------------------------
TOTAL                         5414   3969    73%
----------------------------------------------------------------------
Ran 371 tests in 37.550s

FAILED (failures=1)

特にエラーがなければサーバを起動します。
こちらの環境では上記のようにAssertionError?が出て、FAILEDになっていますが
起動等は問題なく行えます。

# startmain

rootユーザ以外だと、"Unable to increase file descriptor limit. Running as non-root?"と出ますが問題なく起動します。

# startrest

updaterなども同様に起動させます。

アカウントの作成

テスト用のswiftのアカウントを作成します。
devauthの値は、auth-serverに設定されているものにします。

# swift-auth-add-user -K devauth test tester testing

curlでのテスト

アカウント作成後、X-Storage-UrlとX-Auth-Tokenを取得します。

# curl -v -H 'X-Storage-User: test:tester' -H 'X-Storage-Pass: testing'  http://127.0.0.1:11000/v1.0
* About to connect() to 127.0.0.1 port 11000 (#0)
*   Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 11000 (#0)
> GET /v1.0 HTTP/1.1
> User-Agent: curl/7.19.7 (i486-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k  zlib/1.2.3.3 libidn/1.15
> Host: 127.0.0.1:11000
> Accept: */*
> X-Storage-User: test:tester
> X-Storage-Pass: testing
>
< HTTP/1.1 204 No Content
< X-Storage-Url: http://127.0.0.1:9000/v1/ef7a92d3-a959-4dcd-9ab1-82028fd67d6b
< X-Storage-Token: tkc3f4640b-68fa-4ffd-bb09-29d80ee64ab4
< X-Auth-Token: tkc3f4640b-68fa-4ffd-bb09-29d80ee64ab4
< Content-Length: 0
< Date: Wed, 01 Sep 2010 00:07:13 GMT
<
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

出力結果から、X-Storage-UrlとX-Auth-Tokenを取り出し、その後の操作に使用します。

以下のようにしてHTTPのGETメソッドでテストを行ないます。

# curl -v -H 'X-Auth-Token: tkc3f4640b-68fa-4ffd-bb09-29d80ee64ab4'  http://127.0.0.1:8080/v1/ef7a92d3-a959-4dcd-9ab1-82028fd67d6b
* 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 /v1/ef7a92d3-a959-4dcd-9ab1-82028fd67d6b HTTP/1.1
> User-Agent: curl/7.19.7 (i486-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k  zlib/1.2.3.3 libidn/1.15
> Host: 127.0.0.1:8080
> Accept: */*
> X-Auth-Token: tkc3f4640b-68fa-4ffd-bb09-29d80ee64ab4
>
< HTTP/1.1 204 No Content
< X-Container-Object-Count: 0
< X-Container-Bytes-Used: 0
< Content-Length: 0
< Date: Wed, 01 Sep 2010 00:11:51 GMT
<
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

この時点ではまだ何もオブジェクトがないため、No Contentと表示されます。

他にもPUTメソッドでディレクトリの作成やファイルのアップロードが行なえます。
ディレクトリの作成は、以下のように、-XオプションでPUTメソッドを指定し、作成するディレクトリ名をURLの末尾に追加します。

# curl -X PUT -v -H 'X-Auth-Token: tkc3f4640b-68fa-4ffd-bb09-29d80ee64ab4'   http://127.0.0.1:8080/v1/ef7a92d3-a959-4dcd-9ab1-82028fd67d6b/aaaa
* 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)
> PUT /v1/ef7a92d3-a959-4dcd-9ab1-82028fd67d6b/aaaa HTTP/1.1
> User-Agent: curl/7.19.7 (i486-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k  zlib/1.2.3.3 libidn/1.15
> Host: 127.0.0.1:8080
> Accept: */*
> X-Auth-Token: tkc3f4640b-68fa-4ffd-bb09-29d80ee64ab4
>
< HTTP/1.1 201 Created
< Content-Length: 18
< Content-Type: text/plain; charset=UTF-8
< Date: Wed, 01 Sep 2010 00:17:51 GMT
<
201 Created



* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

201 Createdがでれば成功です。
既にディレクトリがある場合は202 Acceptedと出るようです。

ファイルのアップロードは以下のようにします。

# curl -X PUT -T test -v -H 'X-Auth-Token: tkc3f4640b-68fa-4ffd-bb09-29d80ee64ab4'  http://127.0.0.1:8080/v1/ef7a92d3-a959-4dcd-9ab1-82028fd67d6b/aaaa/
* 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)
> PUT /v1/ef7a92d3-a959-4dcd-9ab1-82028fd67d6b/aaaa/test HTTP/1.1
> User-Agent: curl/7.19.7 (i486-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k  zlib/1.2.3.3 libidn/1.15
> Host: 127.0.0.1:8080
> Accept: */*
> X-Auth-Token: tkc3f4640b-68fa-4ffd-bb09-29d80ee64ab4
> Content-Length: 17
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
< HTTP/1.1 201 Created
< Content-Length: 118
< Content-Type: text/plain; charset=UTF-8
< Etag: 7276c6ddf848dc69460b7873bd6ddb77
< Last-Modified: Wed, 01 Sep 2010 00:22:20 GMT
< Date: Wed, 01 Sep 2010 00:22:20 GMT
<
<html>
 <head>
  <title>201 Created</title>
 </head>
 <body>
  <h1>201 Created</h1>
  <br /><br />



 </body>
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

アップロードしたファイルは、GETメソッドで確認できます。
hogehogeの部分に、ファイルの内容が表示されます。

#  curl -v -H 'X-Auth-Token: tkc3f4640b-68fa-4ffd-bb09-29d80ee64ab4'  http://127.0.0.1:8080/v1/ef7a92d3-a959-4dcd-9ab1-82028fd67d6b/aaaa/test
* 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 /v1/ef7a92d3-a959-4dcd-9ab1-82028fd67d6b/aaaa/test HTTP/1.1
> User-Agent: curl/7.19.7 (i486-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15
> Host: 127.0.0.1:8080
> Accept: */*
> X-Auth-Token: tkc3f4640b-68fa-4ffd-bb09-29d80ee64ab4
>
< HTTP/1.1 200 OK
< Last-Modified: Wed, 01 Sep 2010 00:22:20 GMT
< Etag: 7276c6ddf848dc69460b7873bd6ddb77
< Content-Length: 17
< Content-Type: application/octet-stream
< Date: Wed, 01 Sep 2010 00:31:55 GMT
<
hogehoge
hogege

* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

stという、Swiftに含まれているコマンドでもテストを行ないます。

# st -A http://127.0.0.1:11000/v1.0 -U test:tester -K testing stat
   Account: AUTH_dcff3fa2cf494e1e8d211b791b461e4f
Containers: 1
   Objects: 1
    Bytes: 0

アカウントの、利用状況が確認できます。

スクリプトでのテスト

上で説明した、curlでの操作を行うスクリプトを作成してあります。

swift_create_token

#!/bin/bash

curl -v -H "X-Storage-User: $1:$2" -H "X-Storage-Pass: $3"  "http://$4/v1.0" > token_tmp 2>&1
grep X-Auth-Token token_tmp | awk -F ": " '{print $2}' > token
grep X-Storage-Url token_tmp | awk -F ": " '{print $2}' > url
# swift_create_token test tester testing 127.0.0.1

のように実行します。
カレントディレクトリにファイルを作り、X-Storage-UrlとX-Auth-Tokenの値をそれぞれに保存します。

swift_getter

#!/bin/bash

TOKEN=`sed -e 's/\r//' token`
URL=`sed -e 's/\r//' url`

curl -v -H "X-Auth-Token: $TOKEN" $URL/$1

swift_create_tokenで作成したファイルからTokenとURLを読み、GETを実行します。
引数にはURLを指定することができて、directory/fileなどと指定すればdirectoryの中にあるfileの内容を表示できます。
省略すれば一番上の階層で、リストを見ることが出来ます。

swift_putter

#!/bin/bash 

TOKEN=`sed -e 's/\r//' token`
URL=`sed -e 's/\r//' url`

curl -v -X PUT -H "X-Auth-Token: $TOKEN" $URL/$1
# swift_putter directory

のように実行し、引数で指定した名前のディレクトリを作成します。

swift_uploader

#!/bin/bash 

TOKEN=`sed -e 's/\r//' token`
URL=`sed -e 's/\r//' url`

curl -v -T $2 -X PUT -H "X-Auth-Token: $TOKEN" $URL/$1/
# swift_uploader directory file

のように実行し、1つ目の引数で指定したディレクトリに、2つ目の引数で指定したファイルをアップロードします。

リンク

Swift All In One


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS