Revision e742badc
b/kamaki/cli.py | ||
---|---|---|
801 | 801 |
size = format_size(container['bytes']) |
802 | 802 |
print('%s (%s, %s objects)' % (container['name'], size, container['count'])) |
803 | 803 |
|
804 |
|
|
805 | 804 |
def main(self, container=None, object=None): |
806 | 805 |
super(store_list, self).main() |
807 | 806 |
if container is None: |
b/kamaki/clients/__init__.py | ||
---|---|---|
5 | 5 |
# conditions are met: |
6 | 6 |
# |
7 | 7 |
# 1. Redistributions of source code must retain the above |
8 |
# copyright notice, this list of conditions and the following
|
|
8 |
# copyright notice, self.list of conditions and the following
|
|
9 | 9 |
# disclaimer. |
10 | 10 |
# |
11 | 11 |
# 2. Redistributions in binary form must reproduce the above |
12 |
# copyright notice, this list of conditions and the following
|
|
12 |
# copyright notice, self.list of conditions and the following
|
|
13 | 13 |
# disclaimer in the documentation and/or other materials |
14 | 14 |
# provided with the distribution. |
15 | 15 |
# |
... | ... | |
61 | 61 |
def __init__(self, base_url, token): |
62 | 62 |
self.base_url = base_url |
63 | 63 |
self.token = token |
64 |
self.headers = {} |
|
64 | 65 |
|
65 | 66 |
def raise_for_status(self, r): |
66 | 67 |
message = "%d %s" % (r.status_code, r.status) |
67 | 68 |
details = r.text |
68 | 69 |
raise ClientError(message, r.status_code, details) |
69 | 70 |
|
71 |
def set_header(self, name, value): |
|
72 |
#If the header exists, replace the value |
|
73 |
#otherwise create a new header |
|
74 |
self.headers[unicode(name)] = unicode(value) |
|
75 |
|
|
70 | 76 |
def request(self, method, path, **kwargs): |
71 | 77 |
raw = kwargs.pop('raw', False) |
72 | 78 |
success = kwargs.pop('success', 200) |
73 | 79 |
directory = kwargs.pop('directory', False) |
74 |
meta = kwargs.pop('meta', False) |
|
80 |
#meta = kwargs.pop('meta', False)
|
|
75 | 81 |
|
76 | 82 |
data = kwargs.pop('data', None) |
77 |
headers = kwargs.pop('headers', {}) |
|
78 |
headers.setdefault('X-Auth-Token', self.token) |
|
83 |
#headers = kwargs.pop('headers', {})
|
|
84 |
self.headers.setdefault('X-Auth-Token', self.token)
|
|
79 | 85 |
publish = kwargs.pop('publish', None) |
80 | 86 |
|
81 | 87 |
if directory: |
82 |
headers.setdefault('Content-Type', 'application/directory') |
|
83 |
headers.setdefault('Content-length', '0') |
|
88 |
self.headers.setdefault('Content-Type', 'application/directory')
|
|
89 |
self.headers.setdefault('Content-length', '0')
|
|
84 | 90 |
else: |
85 | 91 |
if 'json' in kwargs: |
86 | 92 |
data = json.dumps(kwargs.pop('json')) |
87 |
headers.setdefault('Content-Type', 'application/json') |
|
93 |
self.headers.setdefault('Content-Type', 'application/json')
|
|
88 | 94 |
if data: |
89 |
headers.setdefault('Content-Length', unicode(len(data))) |
|
95 |
self.headers.setdefault('Content-Length', unicode(len(data)))
|
|
90 | 96 |
|
91 |
if meta: |
|
92 |
for key in meta.keys(): |
|
93 |
headers[key] = meta[key]
|
|
97 |
#if meta:
|
|
98 |
# for key in meta.keys():
|
|
99 |
# self.headers[key] = meta[key]
|
|
94 | 100 |
|
95 |
if publish is not None: |
|
96 |
headers.setdefault('X-object-public', unicode(publish)) |
|
97 |
|
|
98 | 101 |
url = self.base_url + path |
99 | 102 |
kwargs.setdefault('verify', False) # Disable certificate verification |
100 |
print('HAVE WE BEEN OVER THIS? '+unicode(headers)) |
|
101 |
r = requests.request(method, url, headers=headers, data=data, **kwargs) |
|
103 |
r = requests.request(method, url, headers=self.headers, data=data, **kwargs) |
|
102 | 104 |
|
103 |
print('HAVE WE BEEN OVER THIS?') |
|
104 | 105 |
req = r.request |
105 | 106 |
sendlog.info('%s %s', req.method, req.url) |
106 | 107 |
for key, val in req.headers.items(): |
b/kamaki/clients/pithos.py | ||
---|---|---|
51 | 51 |
|
52 | 52 |
def purge_container(self, container): |
53 | 53 |
self.assert_account() |
54 |
path = '/%s/%s' % (self.account, container) |
|
55 |
params = {'until': int(time())} |
|
56 |
self.delete(path, params=params, success=204) |
|
54 |
path = path4url(self.account, container)+params4url({'until': unicode(time())}) |
|
55 |
self.delete(path, success=204) |
|
57 | 56 |
|
58 | 57 |
def put_block(self, data, hash): |
59 |
path = '/%s/%s' % (self.account, self.container) |
|
60 |
params = {'update': ''} |
|
61 |
headers = {'Content-Type': 'application/octet-stream', |
|
62 |
'Content-Length': str(len(data))} |
|
63 |
r = self.post(path, params=params, data=data, headers=headers, |
|
64 |
success=202) |
|
58 |
path = path4url(self.account, self.container)+params4url({'update':None}) |
|
59 |
self.set_header('Content-Type', 'application/octet-stream') |
|
60 |
self.set_header('Content-Length', len(data)) |
|
61 |
r = self.post(path, data=data, success=202) |
|
65 | 62 |
assert r.text.strip() == hash, 'Local hash does not match server' |
66 | 63 |
|
67 | 64 |
def create_object(self, object, f, size=None, hash_cb=None, |
... | ... | |
78 | 75 |
self.assert_container() |
79 | 76 |
|
80 | 77 |
meta = self.get_container_info(self.container) |
81 |
print(unicode(meta)) |
|
82 | 78 |
blocksize = int(meta['x-container-block-size']) |
83 | 79 |
blockhash = meta['x-container-block-hash'] |
84 | 80 |
|
... | ... | |
105 | 101 |
|
106 | 102 |
assert offset == size |
107 | 103 |
|
108 |
path = '/%s/%s/%s' % (self.account, self.container, object)
|
|
109 |
params = dict(format='json', hashmap='') |
|
104 |
path = path4url(self.account, self.container, object)+params4url(dict(format='json', hashmap=''))
|
|
105 |
|
|
110 | 106 |
hashmap = dict(bytes=size, hashes=hashes) |
111 |
headers = {'Content-Type': 'application/octet-stream'} |
|
112 |
r = self.put(path, params=params, headers=headers, json=hashmap, |
|
113 |
success=(201, 409)) |
|
107 |
self.set_header('Content-Type', 'application/octet-stream') |
|
108 |
r = self.put(path, json=hashmap, success=(201, 409)) |
|
114 | 109 |
|
115 | 110 |
if r.status_code == 201: |
116 | 111 |
return |
... | ... | |
129 | 124 |
if upload_cb: |
130 | 125 |
upload_gen.next() |
131 | 126 |
|
132 |
self.put(path, params=params, headers=headers, json=hashmap,
|
|
127 |
self.put(path, json=hashmap, |
|
133 | 128 |
success=201) |
134 | 129 |
|
135 | 130 |
def get_account_policy(self): |
... | ... | |
142 | 137 |
assert(type(metapairs) is dict) |
143 | 138 |
self.assert_account() |
144 | 139 |
path = path4url(self.account)+params4url({'update':None}) |
145 |
meta = prefix_keys(metapairs, 'X-Account-Meta-') |
|
146 |
self.post(path, meta=meta, success=202) |
|
140 |
for key, val in metapairs.items(): |
|
141 |
self.set_header('X-Account-Meta-'+key, val) |
|
142 |
self.post(path, success=202) |
|
147 | 143 |
|
148 | 144 |
def get_container_policy(self, container): |
149 | 145 |
return filter_in(self.get_container_info(container), 'X-Container-Policy-') |
... | ... | |
158 | 154 |
assert(type(metapairs) is dict) |
159 | 155 |
self.assert_container() |
160 | 156 |
path=path4url(self.account, self.container)+params4url({'update':None}) |
161 |
meta = prefix_keys(metapairs, 'X-Container-Meta-') |
|
162 |
self.post(path, meta=meta, success=202) |
|
157 |
for key, val in metapairs.items(): |
|
158 |
self.set_header('X-Container-Meta-'+key, val) |
|
159 |
self.post(path, success=202) |
|
163 | 160 |
|
164 | 161 |
def delete_container_meta(self, metakey): |
165 | 162 |
headers = self.get_container_info(self.container) |
166 |
new_headers = filter_out(headers, 'x-container-meta-'+metakey, exactMatch = True)
|
|
167 |
if len(new_headers) == len(headers):
|
|
163 |
self.headers = filter_out(headers, 'x-container-meta-'+metakey, exactMatch = True)
|
|
164 |
if len(self.headers) == len(headers):
|
|
168 | 165 |
raise ClientError('X-Container-Meta-%s not found' % metakey, 404) |
169 | 166 |
path = path4url(self.account, self.container) |
170 |
self.post(path, headers=new_headers, success = 202)
|
|
167 |
self.post(path, success = 202) |
|
171 | 168 |
|
172 | 169 |
def replace_container_meta(self, metapairs): |
173 | 170 |
self.assert_container() |
174 | 171 |
path=path4url(self.account, self.container) |
175 |
meta = prefix_keys(metapairs, 'X-Container-Meta-') |
|
176 |
self.post(path, meta=meta, success=202) |
|
172 |
for key, val in metapairs.items(): |
|
173 |
self.set_header('X-Container-Meta-'+key, val) |
|
174 |
self.post(path, success=202) |
|
177 | 175 |
|
178 | 176 |
def set_object_meta(self, object, metapairs): |
179 | 177 |
assert(type(metapairs) is dict) |
180 | 178 |
self.assert_container() |
181 | 179 |
path=path4url(self.account, self.container, object)+params4url({'update':None}) |
182 |
meta = prefix_keys(metapairs, 'X-Object-Meta-') |
|
183 |
self.post(path, meta=meta, success=202) |
|
180 |
for key, val in metapairs.items(): |
|
181 |
self.set_header('X-Object-Meta-'+key, val) |
|
182 |
self.post(path, success=202) |
|
184 | 183 |
|
185 | 184 |
def publish_object(self, object): |
186 | 185 |
self.assert_container() |
187 | 186 |
path = path4url(self.account, self.container, object)+params4url({'update':None}) |
188 |
self.post(path, publish=True, success=202) |
|
187 |
self.set_header('X-Object-Public', True) |
|
188 |
self.post(path, success=202) |
|
189 | 189 |
|
190 | 190 |
def unpublish_object(self, object): |
191 | 191 |
self.assert_container() |
192 | 192 |
path = path4url(self.account, self.container, object)+params4url({'update':None}) |
193 |
self.post(path, publish=False, success=202) |
|
193 |
self.set_header('X-Object-Public', False) |
|
194 |
self.post(path, success=202) |
|
194 | 195 |
|
195 | 196 |
|
196 | 197 |
|
b/kamaki/clients/storage.py | ||
---|---|---|
62 | 62 |
def replace_account_meta(self, metapairs): |
63 | 63 |
self.assert_account() |
64 | 64 |
path = path4url(self.account) |
65 |
meta = prefix_keys(metapairs, 'X-Account-Meta-') |
|
66 |
self.post(path, meta=meta, success=202) |
|
65 |
for key, val in metapairs: |
|
66 |
self.set_header('X-Account-Meta-'+key, val) |
|
67 |
self.post(path, success=202) |
|
67 | 68 |
|
68 | 69 |
def delete_account_meta(self, metakey): |
69 | 70 |
headers = self.get_account_info() |
70 |
new_headers = filter_out(headers, 'X-Account-Meta-'+metakey, exactMatch = True)
|
|
71 |
if len(new_headers) == len(headers):
|
|
71 |
self.headers = filter_out(headers, 'X-Account-Meta-'+metakey, exactMatch = True)
|
|
72 |
if len(self.headers) == len(headers):
|
|
72 | 73 |
raise ClientError('X-Account-Meta-%s not found' % metakey, 404) |
73 | 74 |
path = path4url(self.account) |
74 |
self.post(path, headers=new_headers, success = 202)
|
|
75 |
self.post(path, success = 202) |
|
75 | 76 |
|
76 | 77 |
def create_container(self, container): |
77 | 78 |
self.assert_account() |
... | ... | |
86 | 87 |
r = self.head(path, success=(204, 404)) |
87 | 88 |
if r.status_code == 404: |
88 | 89 |
raise ClientError("Container does not exist", r.status_code) |
89 |
reply = r.headers |
|
90 |
return reply |
|
90 |
return r.headers |
|
91 | 91 |
|
92 | 92 |
def delete_container(self, container): |
93 | 93 |
self.assert_account() |
... | ... | |
100 | 100 |
|
101 | 101 |
def list_containers(self): |
102 | 102 |
self.assert_account() |
103 |
path = path4url(self.account) |
|
104 |
params = dict(format='json') |
|
105 |
r = self.get(path, params = params, success = (200, 204)) |
|
103 |
path = path4url(self.account)+params4url(dict(format='json')) |
|
104 |
r = self.get(path, success = (200, 204)) |
|
106 | 105 |
return r.json |
107 | 106 |
|
108 | 107 |
def create_object(self, object, f, size=None, hash_cb=None, |
... | ... | |
130 | 129 |
|
131 | 130 |
def delete_object_meta(self, metakey, object): |
132 | 131 |
headers = self.get_object_info(object) |
133 |
new_headers = filter_out(headers, 'X-Object-Meta-'+metakey, exactMatch = True)
|
|
134 |
if len(new_headers) == len(headers):
|
|
132 |
self.headers = filter_out(headers, 'X-Object-Meta-'+metakey, exactMatch = True)
|
|
133 |
if len(self.headers) == len(headers):
|
|
135 | 134 |
raise ClientError('X-Object-Meta-%s not found' % metakey, 404) |
136 | 135 |
path = path4url(self.account, self.container, object) |
137 |
print('HAVE WE BEEN OVER THIS?') |
|
138 |
print(unicode(new_headers)) |
|
139 |
print('HAVE WE BEEN OVER THIS?') |
|
140 |
self.post(path, headers=new_headers, success = 202) |
|
136 |
self.post(path, success = 202) |
|
141 | 137 |
|
142 |
def replace_object(self, metapairs): |
|
138 |
def replace_object_meta(self, metapairs):
|
|
143 | 139 |
self.assert_container() |
144 | 140 |
path=path4url(self.account, self.container) |
145 |
meta = prefix_keys(metapairs, 'X-Object-Meta-') |
|
146 |
self.post(path, meta=meta, success=202) |
|
141 |
for key, val in metapairs: |
|
142 |
self.set_header('X-Object-Meta-'+key, val) |
|
143 |
self.post(path, success=202) |
|
147 | 144 |
|
148 | 145 |
def get_object(self, object): |
149 | 146 |
self.assert_container() |
... | ... | |
162 | 159 |
def list_objects(self): |
163 | 160 |
self.assert_container() |
164 | 161 |
path = path4url(self.account, self.container) |
165 |
params = dict(format='json') |
|
162 |
params = dict(format='json') #request parameters
|
|
166 | 163 |
r = self.get(path, params=params, success=(200, 204, 404)) |
167 | 164 |
if r.status_code == 404: |
168 | 165 |
raise ClientError("Incorrect account (%s) for that container"%self.account, r.status_code) |
166 |
print(unicode(r.json)) |
|
169 | 167 |
return r.json |
170 | 168 |
|
171 | 169 |
def list_objects_in_path(self, path_prefix): |
172 | 170 |
self.assert_container() |
173 | 171 |
path = path4url(self.account, self.container) |
174 |
params = dict(format='json', path=path_prefix) |
|
172 |
params = dict(format='json', path=path_prefix) #request parameters
|
|
175 | 173 |
r = self.get(path, params=params, success=(200, 204, 404)) |
176 | 174 |
if r.status_code == 404: |
177 | 175 |
raise ClientError("Incorrect account (%s) for that container"%self.account, r.status_code) |
b/kamaki/clients/utils.py | ||
---|---|---|
69 | 69 |
""" |
70 | 70 |
assert(type(params) is dict) |
71 | 71 |
result = '' |
72 |
delimiter = '?'
|
|
72 |
dlmtr = '?'
|
|
73 | 73 |
for name in params: |
74 |
result = result + delimiter + name
|
|
74 |
result = result + dlmtr + name
|
|
75 | 75 |
result = result + '=' + params[name] if params[name] is not None else result |
76 |
delimeter = '&'
|
|
76 |
dlmtr = '&'
|
|
77 | 77 |
return result |
78 | 78 |
|
Also available in: Unified diff