Package yakumo :: Package swift :: Package v1 :: Module file_object
[hide private]
[frames] | no frames]

Source Code for Module yakumo.swift.v1.file_object

  1  # Copyright 2014-2017 by Akira Yoshiyama <akirayoshiyama@gmail.com>. 
  2  # All Rights Reserved. 
  3  # 
  4  #    Licensed under the Apache License, Version 2.0 (the "License"); you may 
  5  #    not use this file except in compliance with the License. You may obtain 
  6  #    a copy of the License at 
  7  # 
  8  #         http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  #    Unless required by applicable law or agreed to in writing, software 
 11  #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 12  #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 13  #    License for the specific language governing permissions and limitations 
 14  #    under the License. 
 15   
 16  """ 
 17  Resource class and its manager for objects on Object Storage V1 API 
 18  """ 
 19   
 20  from yakumo import base 
 21  from yakumo.constant import UNDEF 
 22  from yakumo import mapper 
 23  from yakumo import utils 
 24   
 25   
 26  ATTRIBUTE_MAPPING = [ 
 27      ('name', 'name', mapper.Noop), 
 28      ('content_disposition', 'content-disposition', mapper.Noop), 
 29      ('content_encoding', 'content-encoding', mapper.Noop), 
 30      ('content_type', 'content-type', mapper.Noop), 
 31      ('delete_at', 'x-delete-at', mapper.DateTime), 
 32      ('delete_after', 'x-delete-after', mapper.IntStr), 
 33      ('etag', 'etag', mapper.Noop), 
 34      ('if_none_match', 'if-none-match', mapper.Noop), 
 35      ('modified_at', 'last-modified', mapper.DateTime), 
 36      ('object_count', 'x-container-object-count', mapper.IntStr), 
 37      ('object_manifest', 'x-object-manifest', mapper.Noop), 
 38      ('size', 'content-length', mapper.IntStr), 
 39      ('static_large_object', 'x-static-large-object', mapper.Noop), 
 40      ('timestamp', 'x-timestamp', mapper.FloatStr), 
 41      ('trans_id', 'x-trans-id', mapper.Noop), 
 42      ('trans_id_extra', 'x-trans-id-extra', mapper.Noop), 
 43      ('metadata', 'metadata', mapper.Noop), 
 44      ('data', 'data', mapper.Noop), 
 45  ] 
 46   
 47   
48 -class Resource(base.SwiftV1Resource):
49 """resource class for containers on Object Storage V1 API""" 50
51 - def update(self, content_disposition=UNDEF, content_encoding=UNDEF, 52 content_type=UNDEF, delete_after=UNDEF, delete_at=UNDEF, 53 trans_id_extra=UNDEF, metadata=UNDEF):
54 """ 55 Update metadata of an object 56 57 @keyword content_disposition: Specifies the override behavior for the 58 browser 59 @type content_disposition: str 60 @keyword content_encoding: Content-Encoding metadata 61 @type content_encoding: str 62 @keyword content_type: MIME type for the object 63 @type content_type: str 64 @keyword delete_after: Seconds after which the system removes the 65 object 66 @type delete_after: int 67 @keyword delete_at: When the system removes the object 68 @type delete_at: datetime.datetime 69 @keyword trans_id_extra: Extra transaction information 70 @type trans_id_extra: str 71 @keyword metadata: Key-value style metadata 72 @type metadata: dict 73 @rtype: None 74 """ 75 super(Resource, self).update( 76 content_disposition=content_disposition, 77 content_encoding=content_encoding, 78 content_type=content_type, 79 delete_at=delete_at, 80 delete_after=delete_after, 81 trans_id_extra=trans_id_extra, 82 metadata=metadata)
83
84 - def replace(self, content_disposition=UNDEF, content_encoding=UNDEF, 85 content_type=UNDEF, etag=UNDEF, if_none_match=UNDEF, 86 delete_after=UNDEF, delete_at=UNDEF, object_manifest=UNDEF, 87 size=UNDEF, trans_id_extra=UNDEF, metadata=UNDEF, file=None):
88 """ 89 Replace an object 90 91 @keyword content_disposition: Specifies the override behavior for the 92 browser 93 @type content_disposition: str 94 @keyword content_encoding: Content-Encoding metadata 95 @type content_encoding: str 96 @keyword content_type: MIME type for the object 97 @type content_type: str 98 @keyword etag: MD5 checksum of the object 99 @type etag: str 100 @keyword if_none_match: If-None-Match header 101 @type if_none_match: str 102 @keyword delete_after: When the system removes the object 103 @type delete_after: datetime.datetime 104 @keyword delete_at: When the system removes the object 105 @type delete_at: datetime.datetime 106 @keyword object_manifest: Dynamic large object manifest object 107 @type object_manifest: str 108 @keyword size: Object size 109 @type size: int 110 @keyword trans_id_extra: Extra transaction information 111 @type trans_id_extra: str 112 @keyword metadata: Key-value style metadata 113 @type metadata: dict 114 @keyword file: File name to upload 115 @type file: str 116 @rtype: None 117 """ 118 old_attrs = self.get_attrs() 119 new_attrs = dict( 120 content_disposition=content_disposition, 121 content_encoding=content_encoding, 122 content_type=content_type, 123 object_manifest=object_manifest, 124 delete_after=delete_after, 125 delete_at=delete_at, 126 etag=etag, 127 if_none_match=if_none_match, 128 size=size, 129 trans_id_extra=trans_id_extra, 130 metadata=metadata) 131 for key, value in new_attrs.items(): 132 if value is UNDEF: 133 continue 134 new_attrs[key] = old_attrs[key] 135 136 self._manager.create(self._id, file=file, **new_attrs) 137 self.reload()
138
139 - def copy(self, container=UNDEF, name=UNDEF):
140 """ 141 Copy an object 142 143 @param container: Destination container 144 @type container: swift.container.Resource 145 @param name: Destination object name 146 @type name: str 147 @return: New object 148 @rtype: swift.v1.file_object.Resource 149 """ 150 if container is UNDEF: 151 container = self._manager.parent_resource 152 headers = { 153 "x-copy-from": "/%s/%s" % ( 154 self._manager.parent_resource.get_id(), self._id), 155 } 156 self._http.put_raw(container._manager._url_resource_path, 157 container.get_id(), name, 158 headers=headers) 159 return container.object.get_empty(name)
160
161 - def download(self, file=None):
162 """ 163 Download an object into a file 164 165 @keyword file: File name to save 166 @type file: str 167 @rtype: None 168 """ 169 self._http.get_file(self._url_resource_path, self._id, file=file)
170
171 - def set_metadata(self, **metadata):
172 """ 173 Update metadata of an object 174 175 @keyword metadata: key=value style. 176 @type metadata: dict 177 @rtype: None 178 """ 179 if self.metadata is None: 180 self.metadata = {} 181 self.metadata.update(metadata) 182 self.update()
183
184 - def unset_metadata(self, *keys):
185 """ 186 Delete metadata of an object 187 188 @param key: key of the metadata 189 @type keys: [str] 190 @rtype: None 191 """ 192 if self.metadata is None: 193 return 194 for key in keys: 195 if key in self.metadata: 196 self.metadata.pop(key) 197 self.update()
198 199
200 -class Manager(base.SwiftV1SubManager):
201 """manager class for objects on Object Storage V1 API""" 202 203 resource_class = Resource 204 service_type = 'object-store' 205 _attr_mapping = ATTRIBUTE_MAPPING 206 _has_detail = False 207 _url_resource_path = '/%s' 208 _json_resource_key = 'object' 209
210 - def create(self, name, content_disposition=UNDEF, content_encoding=UNDEF, 211 content_type=UNDEF, etag=UNDEF, if_none_match=UNDEF, 212 delete_after=UNDEF, delete_at=UNDEF, object_manifest=UNDEF, 213 size=UNDEF, trans_id_extra=UNDEF, metadata=UNDEF, file=None):
214 """ 215 Create an object 216 217 @param name: Object name 218 @type name: str 219 @keyword content_disposition: Specifies the override behavior for the 220 browser 221 @type content_disposition: str 222 @keyword content_encoding: Content-Encoding metadata 223 @type content_encoding: str 224 @keyword content_type: MIME type for the object 225 @type content_type: str 226 @keyword etag: MD5 checksum of the object 227 @type etag: str 228 @keyword if_none_match: If-None-Match header 229 @type if_none_match: str 230 @keyword delete_after: When the system removes the object 231 @type delete_after: datetime.datetime 232 @keyword delete_at: When the system removes the object 233 @type delete_at: datetime.datetime 234 @keyword object_manifest: Dynamic large object manifest object 235 @type object_manifest: str 236 @keyword size: Object size 237 @type size: int 238 @keyword trans_id_extra: Extra transaction information 239 @type trans_id_extra: str 240 @keyword metadata: Key-value style metadata 241 @type metadata: dict 242 @keyword file: File name to upload 243 @type file: str 244 @return: Created objects 245 @rtype: yakumo.swift.v1.objects.Resource 246 """ 247 data = UNDEF 248 if file: 249 data = utils.gen_chunk(file) 250 return super(Manager, self).create( 251 name, 252 content_disposition=content_disposition, 253 content_encoding=content_encoding, 254 content_type=content_type, 255 object_manifest=object_manifest, 256 delete_after=delete_after, 257 delete_at=delete_at, 258 etag=etag, 259 if_none_match=if_none_match, 260 size=size, 261 trans_id_extra=trans_id_extra, 262 metadata=metadata, 263 data=data)
264