Răsfoiți Sursa

Add size and last_modified properties to bucket object class + tests

Enis Afgan 10 ani în urmă
părinte
comite
b77a41f373

+ 20 - 0
cloudbridge/cloud/interfaces/resources.py

@@ -1827,6 +1827,26 @@ class BucketObject(CloudResource):
         """
         pass
 
+    @abstractproperty
+    def size(self):
+        """
+        Get this object's size.
+
+        :rtype: int
+        :return: Size of this object in bytes.
+        """
+        pass
+
+    @abstractproperty
+    def last_modified(self):
+        """
+        Get the date and time this object was last modified.
+
+        :rtype: str
+        :return: Date and time formatted string %Y-%m-%dT%H:%M:%S.%f
+        """
+        pass
+
     @abstractmethod
     def iter_content(self):
         """

+ 16 - 0
cloudbridge/cloud/providers/aws/resources.py

@@ -22,6 +22,7 @@ from cloudbridge.cloud.interfaces.resources import MachineImageState
 from cloudbridge.cloud.interfaces.resources import NetworkState
 from cloudbridge.cloud.interfaces.resources import SnapshotState
 from cloudbridge.cloud.interfaces.resources import VolumeState
+from datetime import datetime
 import hashlib
 import inspect
 import json
@@ -745,6 +746,21 @@ class AWSBucketObject(BaseBucketObject):
         """
         return self._key.name
 
+    @property
+    def size(self):
+        """
+        Get this object's size.
+        """
+        return self._key.size
+
+    @property
+    def last_modified(self):
+        """
+        Get the date and time this object was last modified.
+        """
+        lm = datetime.strptime(self._key.last_modified, "%Y-%m-%dT%H:%M:%S.%fZ")
+        return lm.strftime("%Y-%m-%dT%H:%M:%S.%f")
+
     def iter_content(self):
         """
         Returns this object's content as an

+ 8 - 0
cloudbridge/cloud/providers/openstack/resources.py

@@ -896,6 +896,14 @@ class OpenStackBucketObject(BaseBucketObject):
         """
         return self._obj.get("name")
 
+    @property
+    def size(self):
+        return self._obj.get("bytes")
+
+    @property
+    def last_modified(self):
+        return self._obj.get("last_modified")
+
     def iter_content(self):
         """
         Returns this object's content as an

+ 11 - 0
test/test_object_store_service.py

@@ -1,3 +1,4 @@
+from datetime import datetime
 from io import BytesIO
 import uuid
 
@@ -95,6 +96,16 @@ class CloudObjectStoreServiceTestCase(ProviderTestBase):
                 obj.upload("dummy content")
                 objs = test_bucket.list()
 
+                self.assertTrue(
+                    isinstance(objs[0].size, int),
+                    "Object size property needs to be a int, not {0}".format(
+                        type(objs[0].size)))
+                self.assertTrue(
+                    datetime.strptime(objs[0].last_modified,
+                                      "%Y-%m-%dT%H:%M:%S.%f"),
+                    "Object's last_modified field format {0} not matching."
+                    .format(objs[0].last_modified))
+
                 # check iteration
                 iter_objs = list(test_bucket)
                 self.assertListEqual(iter_objs, objs)