python-swiftclient api 实例

郗阳德
2023-12-01

python-swiftclient api 实例

1. 获取python-swiftclinet

git clone https://github.com/openstack/python-swiftclient.git

2. 开始使用

#和keystone建立连接
>>> import client
>>> authurl = 'http://192.168.1.61:5000/v2.0'
>>> user = 'admin'
>>> key = 'admin'
>>> auth = client.Connection(authurl, user, key, tenant_name='admin', auth_version="1") 
 
#获取storage url和token
>>> auth.get_auth()   
(u'http://192.168.0.63:8080/v1/AUTH_63aa76aa231f49f9a2ebb7ced54149a8', u'b49221534b7f4dd28cc0df02d6d984d0')
 
#获取account内容
>>> auth.get_account()
({'content-length': '93', 'accept-ranges': 'bytes', 'date': 'Thu, 30 Aug 2012 03:26:42 GMT', 'x-account-bytes-used': '231090678', 'x-account-container-count': '2', 'content-type': 'application/json; charset=utf-8', 'x-account-object-count': '2'}, [{'count': 1, 'bytes': 230817792, 'name': 'glance'}, {'count': 1, 'bytes': 272886, 'name': 'myfiles01'}])
 
#获取container内容
>>> auth.get_container('myfiles01')
({'content-length': '190', 'x-container-object-count': '1', 'accept-ranges': 'bytes', 'x-container-bytes-used': '272886', 'date': 'Thu, 30 Aug 2012 03:30:00 GMT', 'content-type': 'application/json; charset=utf-8'}, [{'bytes': 272886, 'last_modified': '2012-07-25T06:00:39.645610', 'hash': 'fa287fcc92f4157768785c6ae3e308b5', 'name': 'python-webob_1.0.8-1_all.deb', 'content_type': 'application/x-debian-package'}])
 
#获取object内容
>>> auth.get_object('myfiles01', 'python-webob_1.0.8-1_all.deb') #会得到16进制数据
 
#获取account metadata(元数据)
>>> auth.head_account()
{'content-length': '0', 'accept-ranges': 'bytes', 'date': 'Thu, 30 Aug 2012 03:32:23 GMT', 'x-account-bytes-used': '231090678', 'x-account-container-count': '2', 'x-account-object-count': '2'}
 
#获取container metadata
>>> auth.head_container('myfiles01')
{'x-container-bytes-used': '272886', 'date': 'Thu, 30 Aug 2012 03:33:37 GMT', 'accept-ranges': 'bytes', 'x-container-object-count': '1', 'content-length': '0'}
 
#获取object metadata
>>> auth.head_object('myfiles01', 'python-webob_1.0.8-1_all.deb')         
{'content-length': '272886', 'content-type': 'application/x-debian-package', 'accept-ranges': 'bytes', 'last-modified': 'Wed, 25 Jul 2012 06:00:39 GMT', 'etag': 'fa287fcc92f4157768785c6ae3e308b5', 'date': 'Thu, 30 Aug 2012 03:34:42 GMT', 'x-object-meta-mtime': '1343014563.0'}
 
#创建container
>>> auth.put_container('test')
 
#创建object
>>> auth.put_object('test', 'test.py', 'testobject') #这里只是在创建了一个内容为'testobject'的文件test.py
'78c8de9f11870b74f87e1deeffeccb84'
 
#上传文件
>>> auth.put_object('test', 'version.py', open('version.py', 'rb')) #这样就把本地的version.py上传到swift上了
'87cd0682aec0dec94ce2588b43457a8d'

 类似资料: