object_storage.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 GCP. 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(name='my-bucket')[0]
  27. obj = bucket.objects.find(name='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.storage.buckets.get(container)
  50. obj = bucket.objects.create('my_object.txt')
  51. obj.upload_from_file(source)
  52. Generating signed URLs
  53. ----------------------
  54. Signed URLs are a great way to allow users who do not have credentials for
  55. the cloud provider of your choice, to interact with an object within a
  56. storage bucket.
  57. You can generate signed URLs with ``GET`` permissions to allow a user to
  58. get an object.
  59. .. code-block:: python
  60. provider = CloudProviderFactory().create_provider(
  61. ProviderList.AWS,
  62. {'aws_access_key': 'ACCESS_KEY',
  63. 'aws_secret_key': 'SECRET_KEY',
  64. 'aws_session_token': 'MY_SESSION_TOKEN'})
  65. bucket = provider.storage.buckets.get("my-bucket")
  66. obj = bucket.objects.get("my-file.txt")
  67. url = obj.generate_url(expires_in=7200)
  68. You can also generate a signed URL with `PUT``permissions to allow users
  69. to upload files to your storage bucket.
  70. .. code-block:: python
  71. provider = CloudProviderFactory().create_provider(
  72. ProviderList.AWS,
  73. {'aws_access_key': 'ACCESS_KEY',
  74. 'aws_secret_key': 'SECRET_KEY',
  75. 'aws_session_token': 'MY_SESSION_TOKEN'})
  76. bucket = provider.storage.buckets.get("my-bucket")
  77. obj = bucket.objects.create("my-file.txt")
  78. url = obj.generate_url(expires_in=7200, writable=True)
  79. With your signed URL, you or someone on your team can upload a file like this
  80. .. code-block:: python
  81. import requests
  82. content = b"Hello world!"
  83. # Only Azure requires the x-ms-blob-type header to be present, but there's no harm
  84. # in sending this in for all providers.
  85. headers = {'x-ms-blob-type': 'BlockBlob'}
  86. requests.put(url, data=content)