Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move _put_object method from the CloudFiles driver to the base Storag…

…eDriver

class, refactor it and make ClouFiles driver use this method.


git-svn-id: https://svn.apache.org/repos/asf/incubator/libcloud/trunk@1090613 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 5d1b7649ef5a2ca314ded501d0c5e9fb0be909bf 1 parent 39b024c
Tomaz Muraus Kami authored
45 libcloud/storage/base.py
@@ -481,6 +481,51 @@ def _save_object(self, response, obj, destination_path,
481 481
482 482 return True
483 483
  484 + def _upload_object(self, object_name, content_type, upload_func,
  485 + upload_func_kwargs, request_path, request_method='PUT',
  486 + headers=None, file_path=None, iterator=None):
  487 + """
  488 + Helper function for setting common request headers and calling the
  489 + passed in callback which uploads an object.
  490 + """
  491 + headers = headers or {}
  492 + if not content_type:
  493 + if file_path:
  494 + name = file_path
  495 + else:
  496 + name = object_name
  497 + content_type, _ = utils.guess_file_mime_type(name)
  498 +
  499 + if not content_type:
  500 + raise AttributeError(
  501 + 'File content-type could not be guessed and' +
  502 + ' no content_type value provided')
  503 +
  504 + headers = {}
  505 + if iterator:
  506 + headers['Transfer-Encoding'] = 'chunked'
  507 + upload_func_kwargs['chunked'] = True
  508 + else:
  509 + file_size = os.path.getsize(file_path)
  510 + headers['Content-Length'] = file_size
  511 + upload_func_kwargs['chunked'] = False
  512 +
  513 + headers['Content-Type'] = content_type
  514 + response = self.connection.request(request_path,
  515 + method=request_method, data=None,
  516 + headers=headers, raw=True)
  517 +
  518 + upload_func_kwargs['response'] = response
  519 + success, data_hash, bytes_transferred = upload_func(**upload_func_kwargs)
  520 +
  521 + if not success:
  522 + raise LibcloudError(value='Object upload failed, Perhaps a timeout?',
  523 + driver=self)
  524 +
  525 + result_dict = { 'response': response.response, 'data_hash': data_hash,
  526 + 'bytes_transferred': bytes_transferred }
  527 + return result_dict
  528 +
484 529 def _stream_data(self, response, iterator, chunked=False,
485 530 calculate_hash=True, chunk_size=None):
486 531 """
76 libcloud/storage/drivers/cloudfiles.py
@@ -278,12 +278,13 @@ def upload_object(self, file_path, container, object_name, extra=None,
278 278 Note: This will override file with a same name if it already exists.
279 279 """
280 280 upload_func = self._upload_file
281   - upload_func_args = { 'file_path': file_path }
  281 + upload_func_kwargs = { 'file_path': file_path }
282 282
283   - return self._put_object(container=container, file_path=file_path,
284   - object_name=object_name, extra=extra,
  283 + return self._put_object(container=container, object_name=object_name,
285 284 upload_func=upload_func,
286   - upload_func_args=upload_func_args)
  285 + upload_func_kwargs=upload_func_kwargs,
  286 + extra=extra, file_path=file_path,
  287 + file_hash=file_hash)
287 288
288 289 def upload_object_via_stream(self, iterator,
289 290 container, object_name, extra=None):
@@ -291,12 +292,12 @@ def upload_object_via_stream(self, iterator,
291 292 iterator = iter(iterator)
292 293
293 294 upload_func = self._stream_data
294   - upload_func_args = { 'iterator': iterator }
  295 + upload_func_kwargs = { 'iterator': iterator }
295 296
296   - return self._put_object(container=container, iterator=iterator,
297   - object_name=object_name, extra=extra,
  297 + return self._put_object(container=container, object_name=object_name,
298 298 upload_func=upload_func,
299   - upload_func_args=upload_func_args)
  299 + upload_func_kwargs=upload_func_kwargs,
  300 + extra=extra, iterator=iterator)
300 301
301 302 def delete_object(self, obj):
302 303 container_name = self._clean_container_name(obj.container.name)
@@ -313,61 +314,40 @@ def delete_object(self, obj):
313 314
314 315 raise LibcloudError('Unexpected status code: %s' % (response.status))
315 316
316   - def _put_object(self, upload_func, upload_func_args, container, object_name,
317   - extra=None, file_path=None, iterator=None, file_hash=None):
  317 + def _put_object(self, container, object_name, upload_func,
  318 + upload_func_kwargs, extra=None, file_path=None,
  319 + iterator=None, file_hash=None):
  320 + extra = extra or {}
318 321 container_name_cleaned = self._clean_container_name(container.name)
319 322 object_name_cleaned = self._clean_object_name(object_name)
320   -
321   - extra = extra or {}
322 323 content_type = extra.get('content_type', None)
323 324 meta_data = extra.get('meta_data', None)
324 325
325   - if not content_type:
326   - if file_path:
327   - name = file_path
328   - else:
329   - name = object_name
330   - content_type, _ = utils.guess_file_mime_type(name)
331   -
332   - if not content_type:
333   - raise AttributeError(
334   - 'File content-type could not be guessed and' +
335   - ' no content_type value provided')
336   -
337 326 headers = {}
338   - if iterator:
339   - headers['Transfer-Encoding'] = 'chunked'
340   - upload_func_args['chunked'] = True
341   - else:
342   - file_size = os.path.getsize(file_path)
343   - headers['Content-Length'] = file_size
344   - upload_func_args['chunked'] = False
345   -
346   - if file_hash:
347   - headers['ETag'] = file_hash
348   -
349   - headers['Content-Type'] = content_type
  327 + if not iterator and file_hash:
  328 + headers['ETag'] = file_hash
350 329
351 330 if meta_data:
352 331 for key, value in meta_data.iteritems():
353 332 key = 'X-Object-Meta-%s' % (key)
354 333 headers[key] = value
355 334
356   - response = self.connection.request('/%s/%s' % (container_name_cleaned,
357   - object_name_cleaned),
358   - method='PUT', data=None,
359   - headers=headers, raw=True)
360   -
361   - upload_func_args['response'] = response
362   - success, data_hash, bytes_transferred = upload_func(**upload_func_args)
  335 + request_path = '/%s/%s' % (container_name_cleaned, object_name_cleaned)
  336 + result_dict = self._upload_object(object_name=object_name,
  337 + content_type=content_type,
  338 + upload_func=upload_func,
  339 + upload_func_kwargs=upload_func_kwargs,
  340 + request_path=request_path,
  341 + request_method='PUT',
  342 + headers=headers, file_path=file_path,
  343 + iterator=iterator)
363 344
364   - if not success:
365   - raise LibcloudError('Object upload failed, Perhaps a timeout?')
366   -
367   - response = response.response
  345 + response = result_dict['response']
  346 + bytes_transferred = result_dict['bytes_transferred']
368 347
369 348 if response.status == httplib.EXPECTATION_FAILED:
370   - raise LibcloudError('Missing content-type header')
  349 + raise LibcloudError(value='Missing content-type header',
  350 + driver=self)
371 351 elif response.status == httplib.UNPROCESSABLE_ENTITY:
372 352 raise ObjectHashMismatchError(
373 353 value='MD5 hash checksum does not match',

0 comments on commit 5d1b764

Please sign in to comment.
Something went wrong with that request. Please try again.