object_storage.rst 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Working with object storage
  2. ==========================
  3. Object storage provides a simple way to store and retrieve large amounts of
  4. unstructured data over HTTP. Object Storage is also referred to as Blob (Binary
  5. Large OBject) Storage by Azure, and Simple Storage Service (S3) by Amazon.
  6. Typically, you would store your objects within a Bucket, as it is known in
  7. AWS and GCE. A Bucket is also called a Container in OpenStack and Azure. In
  8. CloudBridge, we use the term Bucket.
  9. Storing objects in a bucket
  10. ---------------------------
  11. To store an object within a bucket, we need to first create a bucket or
  12. retrieve an existing bucket.
  13. .. code-block:: python
  14. bucket = provider.storage.buckets.create('my-bucket')
  15. bucket.objects.list()
  16. Next, let's upload some data to this bucket. To efficiently upload a file,
  17. simple use the upload_from_file method.
  18. .. code-block:: python
  19. obj = bucket.objects.create('my-data.txt')
  20. obj.upload_from_file('/path/to/myfile.txt')
  21. You can also use the upload() function to upload from an in memory stream.
  22. Note that, an object you create with objects.create() doesn't actually get
  23. persisted until you upload some content.
  24. To locate and download this uploaded file again, you can do the following:
  25. .. code-block:: python
  26. bucket = provider.storage.buckets.find('my-bucket')[0]
  27. obj = bucket.objects.find('my-data.txt')[0]
  28. print("Size: {0}, Modified: {1}".format(obj.size, obj.last_modified))
  29. with open('/tmp/myfile.txt', 'wb') as f:
  30. obj.save_content(f)
  31. Using tokens for authentication
  32. -------------------------------
  33. Some providers may support using temporary credentials with a session token,
  34. in which case you will be able to access a particular bucket by using that
  35. session token.
  36. .. code-block:: python
  37. provider = CloudProviderFactory().create_provider(
  38. ProviderList.AWS,
  39. {'aws_access_key': 'ACCESS_KEY',
  40. 'aws_secret_key': 'SECRET_KEY',
  41. 'aws_session_token': 'MY_SESSION_TOKEN'})
  42. .. code-block:: python
  43. provider = CloudProviderFactory().create_provider(
  44. ProviderList.OPENSTACK,
  45. {'os_storage_url': 'SWIFT_STORAGE_URL',
  46. 'os_auth_token': 'MY_SESSION_TOKEN'})
  47. Once a provider is obtained, you can access the container as usual:
  48. .. code-block:: python
  49. bucket = provider.object_store.get(container)
  50. obj = bucket.create_object('my_object.txt')
  51. obj.upload_from_file(source)