swiftclientを用いたOpenStack Swift APIの簡単な使い方メモ

## Sessionを使った認証

```
from keystoneauth1 import session
from keystoneauth1.identity import v3
from swiftclient import client

username = 'admin'
tenant_name = 'admin'
password = 'password'
auth_url='http://192.x.x.x/identity/v3'

auth = v3.Password(auth_url=auth_url,
    username=username,
    password=password,
    project_name=tenant_name,
    user_domain_id='default',
    project_domain_id='default')

sess = session.Session(auth=auth)

swift_conn = client.Connection(session=sess)
```

## containerの取得

例外処理も一応書いてみたが...メモ程度

```
try:
    resp_headers, containers = swift_conn.get_account()

except session.exceptions.Unauthorized:
    print "The request you have made requires authentication."

except client.ClientException:
    print "Account GET failed"

print(containers)
```

bpythonで表示されたメソッドの情報．

```
swift_conn.get_account: (self, marker=None, limit=None, prefix=None,      
end_marker=None, full_listing=False, headers=None)                        
get_account                                                               
Wrapper for :func:`get_account`                                           
```

## containerの作成

```
container = 'new-container'
swift_conn.put_container(container)
```

bpythonで表示されたメソッドの情報．
`container` は必須．

```

swift_conn.put_container: (self, container, headers=None,                
response_dict=None, query_string=None)                                   
put_container                                                            
Wrapper for :func:`put_container`                                        

```

## containerにアップロード

```
container = 'new-container'
with open('local.txt', 'r') as local:
    swift_conn.put_object(
        container=container,
        obj='local_object.txt',
        contents=local,
        content_type='text/plain'
    )
```

bpythonで表示されたメソッドの情報．
`container, obj, contents` は必須．

```

swift_conn.put_object: (self, container, obj, contents,                  
content_length=None, etag=None, chunk_size=None, content_type=None,      
headers=None, query_string=None, response_dict=None)                     
put_object                                                               
Wrapper for :func:`put_object`                                           

```

## containerからダウンロード

```
obj = 'local_object.txt
container = 'new-container'
resp_headers, obj_contents = swift_conn.get_object(container, obj)
with open('local_copy.txt','w') as local:
    local.write(obj_contents)
```

bpythonで表示されたメソッドの情報．
`container, obj` は必須．

```

swift_conn.get_object: (self, container, obj, resp_chunk_size=None,      
query_string=None, response_dict=None, headers=None)                     
get_object                                                               
Wrapper for :func:`get_object`                                           

```

## containerからオブジェクトを削除

```
obj = 'local_object.txt'
container = 'new-container'
try:
    swift_conn.delete_object(container, obj)
    print("Successfully deleted the object")
except ClientException as e:
    print("Failed to delete the object with error: %s" % e)
```

bpythonで表示されたメソッドの情報．
`container, obj` は必須．

```

swift_conn.delete_object: (self, container, obj, query_string=None,      
response_dict=None, headers=None)                                        
delete_object                                                            
Wrapper for :func:`delete_object`                                        

```

## フォルダ編(おまけ)
### containerにフォルダを作成

`obj` で通常パスを指定するが，末尾が `/` で終わるパスはフォルダであると認識され
コンテナ内にフォルダが作成される？？
Swiftあんまり分かって無くてすみません(´・ω・｀)

```
container = 'new-container'
swift_conn.put_object(
    container=container,
    obj="folder/",
    contents=None,
    content_type="application/octet-stream"
    )
```

### フォルダの中にファイルをアップロード


```
container = 'new-container'
with open('local.txt', 'r') as local:
    swift_conn.put_object(
        container=container,
        obj='folder/local_object.txt',
        contents=local,
        content_type='text/plain'
    )
```