You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2013/08/13 19:30:10 UTC

[1/2] git commit: [LIBCLOUD-369] Add extension method for listing container objects with prefix

Updated Branches:
  refs/heads/trunk 9e6863c0c -> 4f5e2ef14


[LIBCLOUD-369] Add extension method for listing container objects with prefix

Swift and S3 support a prefix parameter when listing a container
to narrow down the result.

Signed-off-by: Tomaz Muraus <to...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/8929aab4
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/8929aab4
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/8929aab4

Branch: refs/heads/trunk
Commit: 8929aab4512bbe05e79a6d632bdd69e4fc28f736
Parents: 9e6863c
Author: Stefan Friesel <sf...@cloudcontrol.de>
Authored: Tue Aug 13 14:57:46 2013 +0200
Committer: Tomaz Muraus <to...@apache.org>
Committed: Tue Aug 13 18:57:40 2013 +0200

----------------------------------------------------------------------
 libcloud/storage/drivers/cloudfiles.py   | 32 +++++++++++++++++++++++++-
 libcloud/storage/drivers/s3.py           | 33 ++++++++++++++++++++++++++-
 libcloud/test/storage/test_cloudfiles.py | 18 +++++++++++++++
 libcloud/test/storage/test_s3.py         | 14 ++++++++++++
 4 files changed, 95 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/8929aab4/libcloud/storage/drivers/cloudfiles.py
----------------------------------------------------------------------
diff --git a/libcloud/storage/drivers/cloudfiles.py b/libcloud/storage/drivers/cloudfiles.py
index db38669..3ca6adb 100644
--- a/libcloud/storage/drivers/cloudfiles.py
+++ b/libcloud/storage/drivers/cloudfiles.py
@@ -644,8 +644,38 @@ class CloudFilesStorageDriver(StorageDriver, OpenStackDriverMixin):
 
         return obj
 
-    def iterate_container_objects(self, container):
+    def list_container_objects(self, container, ex_prefix=None):
+        """
+        Return a list of objects for the given container.
+
+        :param container: Container instance.
+        :type container: :class:`Container`
+
+        :param ex_prefix: Only get objects with names starting with ex_prefix
+        :type ex_prefix: ``str``
+
+        :return: A list of Object instances.
+        :rtype: ``list`` of :class:`Object`
+        """
+        return list(self.iterate_container_objects(container,
+            ex_prefix=ex_prefix))
+
+    def iterate_container_objects(self, container, ex_prefix=None):
+        """
+        Return a generator of objects for the given container.
+
+        :param container: Container instance
+        :type container: :class:`Container`
+
+        :param ex_prefix: Only get objects with names starting with ex_prefix
+        :type ex_prefix: ``str``
+
+        :return: A generator of Object instances.
+        :rtype: ``generator`` of :class:`Object`
+        """
         params = {}
+        if ex_prefix:
+            params['prefix'] = ex_prefix
 
         while True:
             response = self.connection.request('/%s' % (container.name),

http://git-wip-us.apache.org/repos/asf/libcloud/blob/8929aab4/libcloud/storage/drivers/s3.py
----------------------------------------------------------------------
diff --git a/libcloud/storage/drivers/s3.py b/libcloud/storage/drivers/s3.py
index 38bd723..db22c7f 100644
--- a/libcloud/storage/drivers/s3.py
+++ b/libcloud/storage/drivers/s3.py
@@ -227,8 +227,39 @@ class S3StorageDriver(StorageDriver):
         raise LibcloudError('Unexpected status code: %s' % (response.status),
                             driver=self)
 
-    def iterate_container_objects(self, container):
+    def list_container_objects(self, container, ex_prefix=None):
+        """
+        Return a list of objects for the given container.
+
+        :param container: Container instance.
+        :type container: :class:`Container`
+
+        :param ex_prefix: Only return objects starting with ex_prefix
+        :type ex_prefix: ``str``
+
+        :return: A list of Object instances.
+        :rtype: ``list`` of :class:`Object`
+        """
+        return list(self.iterate_container_objects(container,
+            ex_prefix=ex_prefix))
+
+    def iterate_container_objects(self, container, ex_prefix=None):
+        """
+        Return a generator of objects for the given container.
+
+        :param container: Container instance
+        :type container: :class:`Container`
+
+        :param ex_prefix: Only return objects starting with ex_prefix
+        :type ex_prefix: ``str``
+
+        :return: A generator of Object instances.
+        :rtype: ``generator`` of :class:`Object`
+        """
         params = {}
+        if ex_prefix:
+            params['prefix'] = ex_prefix
+
         last_key = None
         exhausted = False
         container_path = self._get_container_path(container)

http://git-wip-us.apache.org/repos/asf/libcloud/blob/8929aab4/libcloud/test/storage/test_cloudfiles.py
----------------------------------------------------------------------
diff --git a/libcloud/test/storage/test_cloudfiles.py b/libcloud/test/storage/test_cloudfiles.py
index 524d549..e5e0330 100644
--- a/libcloud/test/storage/test_cloudfiles.py
+++ b/libcloud/test/storage/test_cloudfiles.py
@@ -188,6 +188,24 @@ class CloudFilesTests(unittest.TestCase):
         self.assertEqual(obj.size, 1160520)
         self.assertEqual(obj.container.name, 'test_container')
 
+    def test_list_container_objects_with_prefix(self):
+        CloudFilesMockHttp.type = 'EMPTY'
+        container = Container(
+            name='test_container', extra={}, driver=self.driver)
+        objects = self.driver.list_container_objects(container=container,
+            ex_prefix='test_prefix1')
+        self.assertEqual(len(objects), 0)
+
+        CloudFilesMockHttp.type = None
+        objects = self.driver.list_container_objects(container=container,
+            ex_prefix='test_prefix2')
+        self.assertEqual(len(objects), 4)
+
+        obj = [o for o in objects if o.name == 'foo test 1'][0]
+        self.assertEqual(obj.hash, '16265549b5bda64ecdaa5156de4c97cc')
+        self.assertEqual(obj.size, 1160520)
+        self.assertEqual(obj.container.name, 'test_container')
+
     def test_list_container_objects_iterator(self):
         CloudFilesMockHttp.type = 'ITERATOR'
         container = Container(

http://git-wip-us.apache.org/repos/asf/libcloud/blob/8929aab4/libcloud/test/storage/test_s3.py
----------------------------------------------------------------------
diff --git a/libcloud/test/storage/test_s3.py b/libcloud/test/storage/test_s3.py
index 2287d5c..9de324e 100644
--- a/libcloud/test/storage/test_s3.py
+++ b/libcloud/test/storage/test_s3.py
@@ -476,6 +476,20 @@ class S3Tests(unittest.TestCase):
         self.assertTrue(obj in objects)
         self.assertEqual(len(objects), 5)
 
+    def test_list_container_objects_with_prefix(self):
+        self.mock_response_klass.type = None
+        container = Container(name='test_container', extra={},
+                              driver=self.driver)
+        objects = self.driver.list_container_objects(container=container,
+            ex_prefix='test_prefix')
+        self.assertEqual(len(objects), 1)
+
+        obj = [o for o in objects if o.name == '1.zip'][0]
+        self.assertEqual(obj.hash, '4397da7a7649e8085de9916c240e8166')
+        self.assertEqual(obj.size, 1234567)
+        self.assertEqual(obj.container.name, 'test_container')
+        self.assertTrue('owner' in obj.meta_data)
+
     def test_get_container_doesnt_exist(self):
         self.mock_response_klass.type = 'list_containers'
         try:


[2/2] git commit: Update changes.

Posted by to...@apache.org.
Update changes.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/4f5e2ef1
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/4f5e2ef1
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/4f5e2ef1

Branch: refs/heads/trunk
Commit: 4f5e2ef141f396f2bfd8751c2a07c522b61bd201
Parents: 8929aab
Author: Tomaz Muraus <to...@apache.org>
Authored: Tue Aug 13 19:16:35 2013 +0200
Committer: Tomaz Muraus <to...@apache.org>
Committed: Tue Aug 13 19:16:35 2013 +0200

----------------------------------------------------------------------
 CHANGES | 5 +++++
 1 file changed, 5 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/4f5e2ef1/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index f6b534d..c56c4d4 100644
--- a/CHANGES
+++ b/CHANGES
@@ -52,6 +52,11 @@ Changes with Apache Libcloud in development
       Reported by Ben Meng (LIBCLOUD-366)
       [Tomaz Muraus]
 
+    - Allow users to filter objects starting with a prefix by passing ex_prefix
+      argument to the list_container_objects method in the S3, Google Storage
+      and CloudFiles driver. (LIBCLOUD-369)
+      [Stefan Friesel]
+
 Changes with Apache Libcloud 0.13.0:
 
  *) General