Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / storage.py @ a2ef112e

History | View | Annotate | Download (8.3 kB)

1 5cffe0b4 Stavros Sachtouris
#a Copyright 2011 GRNET S.A. All rights reserved.
2 a1c50326 Giorgos Verigakis
#
3 a1c50326 Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 a1c50326 Giorgos Verigakis
# without modification, are permitted provided that the following
5 a1c50326 Giorgos Verigakis
# conditions are met:
6 a1c50326 Giorgos Verigakis
#
7 a1c50326 Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 a1c50326 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 a1c50326 Giorgos Verigakis
#      disclaimer.
10 a1c50326 Giorgos Verigakis
#
11 a1c50326 Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 a1c50326 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 a1c50326 Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 a1c50326 Giorgos Verigakis
#      provided with the distribution.
15 a1c50326 Giorgos Verigakis
#
16 a1c50326 Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 a1c50326 Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 a1c50326 Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 a1c50326 Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 a1c50326 Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 a1c50326 Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 a1c50326 Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 a1c50326 Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 a1c50326 Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 a1c50326 Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 a1c50326 Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 a1c50326 Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 a1c50326 Giorgos Verigakis
#
29 a1c50326 Giorgos Verigakis
# The views and conclusions contained in the software and
30 a1c50326 Giorgos Verigakis
# documentation are those of the authors and should not be
31 a1c50326 Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 a1c50326 Giorgos Verigakis
# or implied, of GRNET S.A.
33 a1c50326 Giorgos Verigakis
34 c270fe96 Stavros Sachtouris
from kamaki.clients import Client, ClientError
35 3dabe5d2 Stavros Sachtouris
from kamaki.clients.utils import filter_in, filter_out, path4url
36 3dabe5d2 Stavros Sachtouris
37 a1c50326 Giorgos Verigakis
38 6a0b1658 Giorgos Verigakis
class StorageClient(Client):
39 d2cea1e2 Giorgos Verigakis
    """OpenStack Object Storage API 1.0 client"""
40 44b8928d Giorgos Verigakis
41 6a0b1658 Giorgos Verigakis
    def __init__(self, base_url, token, account=None, container=None):
42 6ad245d5 Stavros Sachtouris
        super(StorageClient, self).__init__(base_url, token)
43 6a0b1658 Giorgos Verigakis
        self.account = account
44 6a0b1658 Giorgos Verigakis
        self.container = container
45 44b8928d Giorgos Verigakis
46 6a0b1658 Giorgos Verigakis
    def assert_account(self):
47 6a0b1658 Giorgos Verigakis
        if not self.account:
48 2f749e6e Stavros Sachtouris
            raise ClientError("No account provided")
49 44b8928d Giorgos Verigakis
50 6a0b1658 Giorgos Verigakis
    def assert_container(self):
51 6a0b1658 Giorgos Verigakis
        self.assert_account()
52 6a0b1658 Giorgos Verigakis
        if not self.container:
53 2f749e6e Stavros Sachtouris
            raise ClientError("No container provided")
54 44b8928d Giorgos Verigakis
55 d2dec8b1 Stavros Sachtouris
    def get_account_info(self):
56 d2dec8b1 Stavros Sachtouris
        self.assert_account()
57 af3b2b36 Stavros Sachtouris
        path = path4url(self.account)
58 d2dec8b1 Stavros Sachtouris
        r = self.head(path, success=(204, 401))
59 d2dec8b1 Stavros Sachtouris
        if r.status_code == 401:
60 d2dec8b1 Stavros Sachtouris
            raise ClientError("No authorization")
61 5b263ba2 Stavros Sachtouris
        reply = r.headers
62 5b263ba2 Stavros Sachtouris
        return reply
63 d2dec8b1 Stavros Sachtouris
64 af3b2b36 Stavros Sachtouris
    def replace_account_meta(self, metapairs):
65 0e4bec91 Stavros Sachtouris
        self.assert_account()
66 af3b2b36 Stavros Sachtouris
        path = path4url(self.account)
67 3dabe5d2 Stavros Sachtouris
        for key, val in metapairs:
68 3dabe5d2 Stavros Sachtouris
            self.set_header('X-Account-Meta-' + key, val)
69 5b263ba2 Stavros Sachtouris
        r = self.post(path, success=202)
70 3dabe5d2 Stavros Sachtouris
        r.release()
71 0e4bec91 Stavros Sachtouris
72 379cd4bb Stavros Sachtouris
    def del_account_meta(self, metakey):
73 e6b39366 Stavros Sachtouris
        headers = self.get_account_info()
74 3dabe5d2 Stavros Sachtouris
        self.headers = filter_out(headers,
75 3dabe5d2 Stavros Sachtouris
            'X-Account-Meta-' + metakey,
76 3dabe5d2 Stavros Sachtouris
            exactMatch=True)
77 e742badc Stavros Sachtouris
        if len(self.headers) == len(headers):
78 e6b39366 Stavros Sachtouris
            raise ClientError('X-Account-Meta-%s not found' % metakey, 404)
79 e6b39366 Stavros Sachtouris
        path = path4url(self.account)
80 3dabe5d2 Stavros Sachtouris
        r = self.post(path, success=202)
81 3dabe5d2 Stavros Sachtouris
        r.release()
82 0e4bec91 Stavros Sachtouris
83 60560d7c Giorgos Verigakis
    def create_container(self, container):
84 6a0b1658 Giorgos Verigakis
        self.assert_account()
85 af3b2b36 Stavros Sachtouris
        path = path4url(self.account, container)
86 6a0b1658 Giorgos Verigakis
        r = self.put(path, success=(201, 202))
87 6a0b1658 Giorgos Verigakis
        if r.status_code == 202:
88 16d445fe Giorgos Verigakis
            raise ClientError("Container already exists", r.status_code)
89 44b8928d Giorgos Verigakis
90 6c4778ae Stavros Sachtouris
    def get_container_info(self, container):
91 6a0b1658 Giorgos Verigakis
        self.assert_account()
92 af3b2b36 Stavros Sachtouris
        path = path4url(self.account, container)
93 6a0b1658 Giorgos Verigakis
        r = self.head(path, success=(204, 404))
94 6a0b1658 Giorgos Verigakis
        if r.status_code == 404:
95 6a0b1658 Giorgos Verigakis
            raise ClientError("Container does not exist", r.status_code)
96 5b263ba2 Stavros Sachtouris
        reply = r.headers
97 5b263ba2 Stavros Sachtouris
        return reply
98 44b8928d Giorgos Verigakis
99 b66dfee7 Stavros Sachtouris
    def delete_container(self, container):
100 b66dfee7 Stavros Sachtouris
        self.assert_account()
101 af3b2b36 Stavros Sachtouris
        path = path4url(self.account, container)
102 b66dfee7 Stavros Sachtouris
        r = self.delete(path, success=(204, 404, 409))
103 b66dfee7 Stavros Sachtouris
        if r.status_code == 404:
104 b66dfee7 Stavros Sachtouris
            raise ClientError("Container does not exist", r.status_code)
105 b66dfee7 Stavros Sachtouris
        elif r.status_code == 409:
106 b66dfee7 Stavros Sachtouris
            raise ClientError("Container is not empty", r.status_code)
107 b66dfee7 Stavros Sachtouris
108 11f8eed3 Stavros Sachtouris
    def list_containers(self):
109 11f8eed3 Stavros Sachtouris
        self.assert_account()
110 2f749e6e Stavros Sachtouris
        self.set_param('format', 'json')
111 2f749e6e Stavros Sachtouris
        path = path4url(self.account)
112 3dabe5d2 Stavros Sachtouris
        r = self.get(path, success=(200, 204))
113 5b263ba2 Stavros Sachtouris
        reply = r.json
114 5b263ba2 Stavros Sachtouris
        return reply
115 11f8eed3 Stavros Sachtouris
116 65a45524 Stavros Sachtouris
    def upload_object(self, object, f, size=None):
117 6a0b1658 Giorgos Verigakis
        # This is a naive implementation, it loads the whole file in memory
118 8c4f0fbf Stavros Sachtouris
        #Look in pithos for a nice implementation
119 6a0b1658 Giorgos Verigakis
        self.assert_container()
120 af3b2b36 Stavros Sachtouris
        path = path4url(self.account, self.container, object)
121 188f23b9 Giorgos Verigakis
        data = f.read(size) if size is not None else f.read()
122 5b263ba2 Stavros Sachtouris
        r = self.put(path, data=data, success=201)
123 3dabe5d2 Stavros Sachtouris
        r.release()
124 44b8928d Giorgos Verigakis
125 f3a722d4 Stavros Sachtouris
    def create_directory(self, object):
126 f3a722d4 Stavros Sachtouris
        self.assert_container()
127 af3b2b36 Stavros Sachtouris
        path = path4url(self.account, self.container, object)
128 4e68908f Stavros Sachtouris
        self.set_header('Content-Type', 'application/directory')
129 4e68908f Stavros Sachtouris
        self.set_header('Content-length', '0')
130 5b263ba2 Stavros Sachtouris
        r = self.put(path, success=201)
131 3dabe5d2 Stavros Sachtouris
        r.release()
132 f3a722d4 Stavros Sachtouris
133 6c4778ae Stavros Sachtouris
    def get_object_info(self, object):
134 6c4778ae Stavros Sachtouris
        self.assert_container()
135 af3b2b36 Stavros Sachtouris
        path = path4url(self.account, self.container, object)
136 6c4778ae Stavros Sachtouris
        r = self.head(path, success=200)
137 5b263ba2 Stavros Sachtouris
        reply = r.headers
138 5b263ba2 Stavros Sachtouris
        return reply
139 6c4778ae Stavros Sachtouris
140 d0b42fb4 Stavros Sachtouris
    def get_object_meta(self, object):
141 f70616fc Stavros Sachtouris
        r = filter_in(self.get_object_info(object), 'X-Object-Meta-')
142 f70616fc Stavros Sachtouris
        reply = {}
143 f70616fc Stavros Sachtouris
        for (key, val) in r.items():
144 f70616fc Stavros Sachtouris
            metakey = key.split('-')[-1]
145 f70616fc Stavros Sachtouris
            reply[metakey] = val
146 f70616fc Stavros Sachtouris
        return reply
147 e6b39366 Stavros Sachtouris
148 89c2e77b Stavros Sachtouris
    def del_object_meta(self, metakey, object):
149 6de1f262 Stavros Sachtouris
        self.assert_container()
150 3dabe5d2 Stavros Sachtouris
        self.set_header('X-Object-Meta-' + metakey, '')
151 e6b39366 Stavros Sachtouris
        path = path4url(self.account, self.container, object)
152 3dabe5d2 Stavros Sachtouris
        r = self.post(path, success=202)
153 3dabe5d2 Stavros Sachtouris
        r.release()
154 d0b42fb4 Stavros Sachtouris
155 e742badc Stavros Sachtouris
    def replace_object_meta(self, metapairs):
156 af3b2b36 Stavros Sachtouris
        self.assert_container()
157 3dabe5d2 Stavros Sachtouris
        path = path4url(self.account, self.container)
158 e742badc Stavros Sachtouris
        for key, val in metapairs:
159 3dabe5d2 Stavros Sachtouris
            self.set_header('X-Object-Meta-' + key, val)
160 5b263ba2 Stavros Sachtouris
        r = self.post(path, success=202)
161 3dabe5d2 Stavros Sachtouris
        r.release()
162 0e4bec91 Stavros Sachtouris
163 176894c1 Giorgos Verigakis
    def get_object(self, object):
164 6a0b1658 Giorgos Verigakis
        self.assert_container()
165 af3b2b36 Stavros Sachtouris
        path = path4url(self.account, self.container, object)
166 2f749e6e Stavros Sachtouris
        r = self.get(path, success=200)
167 6a0b1658 Giorgos Verigakis
        size = int(r.headers['content-length'])
168 5b263ba2 Stavros Sachtouris
        cnt = r.content
169 5b263ba2 Stavros Sachtouris
        return cnt, size
170 44b8928d Giorgos Verigakis
171 3dabe5d2 Stavros Sachtouris
    def copy_object(self, src_container, src_object, dst_container,
172 3dabe5d2 Stavros Sachtouris
        dst_object=False):
173 c2867610 Stavros Sachtouris
        self.assert_account()
174 c2867610 Stavros Sachtouris
        dst_object = dst_object or src_object
175 c2867610 Stavros Sachtouris
        dst_path = path4url(self.account, dst_container, dst_object)
176 c2867610 Stavros Sachtouris
        self.set_header('X-Copy-From', path4url(src_container, src_object))
177 c2867610 Stavros Sachtouris
        self.set_header('Content-Length', 0)
178 5b263ba2 Stavros Sachtouris
        r = self.put(dst_path, success=201)
179 3dabe5d2 Stavros Sachtouris
        r.release()
180 c2867610 Stavros Sachtouris
181 3dabe5d2 Stavros Sachtouris
    def move_object(self, src_container, src_object, dst_container,
182 3dabe5d2 Stavros Sachtouris
        dst_object=False):
183 eed330ab Stavros Sachtouris
        self.assert_account()
184 eed330ab Stavros Sachtouris
        dst_object = dst_object or src_object
185 eed330ab Stavros Sachtouris
        dst_path = path4url(self.account, dst_container, dst_object)
186 eed330ab Stavros Sachtouris
        self.set_header('X-Move-From', path4url(src_container, src_object))
187 eed330ab Stavros Sachtouris
        self.set_header('Content-Length', 0)
188 5b263ba2 Stavros Sachtouris
        r = self.put(dst_path, success=201)
189 3dabe5d2 Stavros Sachtouris
        r.release()
190 eed330ab Stavros Sachtouris
191 a1c50326 Giorgos Verigakis
    def delete_object(self, object):
192 6a0b1658 Giorgos Verigakis
        self.assert_container()
193 af3b2b36 Stavros Sachtouris
        path = path4url(self.account, self.container, object)
194 bebdd0d6 Stavros Sachtouris
        r = self.delete(path, success=(204, 404))
195 bebdd0d6 Stavros Sachtouris
        if r.status_code == 404:
196 3dabe5d2 Stavros Sachtouris
            raise ClientError("Object %s not found" % object, r.status_code)
197 3dabe5d2 Stavros Sachtouris
198 e2bd16d5 Stavros Sachtouris
    def list_objects(self):
199 2bcb595a Giorgos Verigakis
        self.assert_container()
200 af3b2b36 Stavros Sachtouris
        path = path4url(self.account, self.container)
201 2f749e6e Stavros Sachtouris
        self.set_param('format', 'json')
202 2f749e6e Stavros Sachtouris
        r = self.get(path, success=(200, 204, 304, 404), )
203 175f40ab Stavros Sachtouris
        if r.status_code == 404:
204 3dabe5d2 Stavros Sachtouris
            raise ClientError(\
205 3dabe5d2 Stavros Sachtouris
                "Incorrect account (%s) for that container" % self.account,
206 3dabe5d2 Stavros Sachtouris
                r.status_code)
207 16ce7b91 Stavros Sachtouris
        elif r.status_code == 304:
208 16ce7b91 Stavros Sachtouris
            return []
209 5b263ba2 Stavros Sachtouris
        reply = r.json
210 5b263ba2 Stavros Sachtouris
        return reply
211 e2bd16d5 Stavros Sachtouris
212 e2bd16d5 Stavros Sachtouris
    def list_objects_in_path(self, path_prefix):
213 a79f6d30 Stavros Sachtouris
        self.assert_container()
214 af3b2b36 Stavros Sachtouris
        path = path4url(self.account, self.container)
215 2f749e6e Stavros Sachtouris
        self.set_param('format', 'json')
216 2f749e6e Stavros Sachtouris
        self.set_param('path', 'path_prefix')
217 2f749e6e Stavros Sachtouris
        r = self.get(path, success=(200, 204, 404))
218 a79f6d30 Stavros Sachtouris
        if r.status_code == 404:
219 3dabe5d2 Stavros Sachtouris
            raise ClientError(\
220 3dabe5d2 Stavros Sachtouris
                "Incorrect account (%s) for that container" % self.account,
221 3dabe5d2 Stavros Sachtouris
                r.status_code)
222 5b263ba2 Stavros Sachtouris
        reply = r.json
223 5b263ba2 Stavros Sachtouris
        return reply