소스 검색

ZF-11401: Added the getObjectsAndPrefixesByBucket()

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24095 44c647ce-9c0f-0410-b52a-842ac1e357ba
ezimuel 14 년 전
부모
커밋
a9aa6f9d64
2개의 변경된 파일66개의 추가작업 그리고 6개의 파일을 삭제
  1. 47 2
      library/Zend/Service/Amazon/S3.php
  2. 19 4
      tests/Zend/Service/Amazon/S3/OnlineTest.php

+ 47 - 2
library/Zend/Service/Amazon/S3.php

@@ -160,8 +160,8 @@ class Zend_Service_Amazon_S3 extends Zend_Service_Amazon_Abstract
         $headers=array();
         $headers=array();
         if($location) {
         if($location) {
             $data = '<CreateBucketConfiguration><LocationConstraint>'.$location.'</LocationConstraint></CreateBucketConfiguration>';
             $data = '<CreateBucketConfiguration><LocationConstraint>'.$location.'</LocationConstraint></CreateBucketConfiguration>';
-            $headers['Content-type']= 'text/plain';
-            $headers['Contne-size']= strlen($data);
+            $headers[self::S3_CONTENT_TYPE_HEADER]= 'text/plain';
+            $headers['Content-size']= strlen($data);
         } else {
         } else {
             $data = null;
             $data = null;
         }
         }
@@ -321,7 +321,52 @@ class Zend_Service_Amazon_S3 extends Zend_Service_Amazon_Abstract
 
 
         return $objects;
         return $objects;
     }
     }
+     /**
+     * List the objects and common prefixes in a bucket.
+     *
+     * Provides the list of object keys and common prefixes that are contained in the bucket.  Valid params include the following.
+     * prefix - Limits the response to keys which begin with the indicated prefix. You can use prefixes to separate a bucket into different sets of keys in a way similar to how a file system uses folders.
+     * marker - Indicates where in the bucket to begin listing. The list will only include keys that occur lexicographically after marker. This is convenient for pagination: To get the next page of results use the last key of the current page as the marker.
+     * max-keys - The maximum number of keys you'd like to see in the response body. The server might return fewer than this many keys, but will not return more.
+     * delimiter - Causes keys that contain the same string between the prefix and the first occurrence of the delimiter to be rolled up into a single result element in the CommonPrefixes collection. These rolled-up keys are not returned elsewhere in the response.
+     *
+     * @see ZF-11401
+     * @param  string $bucket
+     * @param array $params S3 GET Bucket Paramater
+     * @return array|false
+     */
+    public function getObjectsAndPrefixesByBucket($bucket, $params = array())
+    {
+        $response = $this->_makeRequest('GET', $bucket, $params);
+
+        if ($response->getStatus() != 200) {
+            return false;
+        }
 
 
+        $xml = new SimpleXMLElement($response->getBody());
+
+        $objects = array();
+        if (isset($xml->Contents)) {
+            foreach ($xml->Contents as $contents) {
+                foreach ($contents->Key as $object) {
+                    $objects[] = (string)$object;
+                }
+            }
+        }
+        $prefixes = array();
+        if (isset($xml->CommonPrefixes)) {
+            foreach ($xml->CommonPrefixes as $prefix) {
+                foreach ($prefix->Prefix as $object) {
+                    $prefixes[] = (string)$object;
+                }
+            }
+        }
+
+        return array(
+            'objects'  => $objects,
+            'prefixes' => $prefixes
+        );
+    }
     /**
     /**
      * Make sure the object name is valid
      * Make sure the object name is valid
      *
      *

+ 19 - 4
tests/Zend/Service/Amazon/S3/OnlineTest.php

@@ -150,7 +150,7 @@ class Zend_Service_Amazon_S3_OnlineTest extends PHPUnit_Framework_TestCase
         $this->assertEquals("testdata", $stream_read, 'Downloaded stream does not seem to match!');
         $this->assertEquals("testdata", $stream_read, 'Downloaded stream does not seem to match!');
         $this->assertEquals("testdata", $file_read, 'Downloaded file does not seem to match!');
         $this->assertEquals("testdata", $file_read, 'Downloaded file does not seem to match!');
     }
     }
-/**
+    /**
      * Test getting info
      * Test getting info
      *
      *
      * @return void
      * @return void
@@ -221,10 +221,10 @@ class Zend_Service_Amazon_S3_OnlineTest extends PHPUnit_Framework_TestCase
         $this->_amazon->putObject($this->_bucket."/zftest", $data);
         $this->_amazon->putObject($this->_bucket."/zftest", $data);
         $this->_amazon->cleanBucket($this->_bucket);
         $this->_amazon->cleanBucket($this->_bucket);
         $this->_amazon->removeBucket($this->_bucket);
         $this->_amazon->removeBucket($this->_bucket);
-
+        
         $this->assertFalse($this->_amazon->isBucketAvailable($this->_bucket));
         $this->assertFalse($this->_amazon->isBucketAvailable($this->_bucket));
         $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket."/zftest"));
         $this->assertFalse($this->_amazon->isObjectAvailable($this->_bucket."/zftest"));
-        $this->assertFalse($this->_amazon->getObjectsByBucket($this->_bucket));
+        $this->assertFalse((boolean)$this->_amazon->getObjectsByBucket($this->_bucket));
         $list = $this->_amazon->getBuckets();
         $list = $this->_amazon->getBuckets();
         $this->assertNotContains($this->_bucket, $list);
         $this->assertNotContains($this->_bucket, $list);
     }
     }
@@ -238,7 +238,6 @@ class Zend_Service_Amazon_S3_OnlineTest extends PHPUnit_Framework_TestCase
         }
         }
 
 
         $data = file_get_contents($filename);
         $data = file_get_contents($filename);
-
         $this->assertTrue($this->_amazon->isObjectAvailable($object));
         $this->assertTrue($this->_amazon->isObjectAvailable($object));
 
 
         $info = $this->_amazon->getInfo($object);
         $info = $this->_amazon->getInfo($object);
@@ -496,6 +495,22 @@ class Zend_Service_Amazon_S3_OnlineTest extends PHPUnit_Framework_TestCase
         $xml = new SimpleXMLElement($response->getBody());
         $xml = new SimpleXMLElement($response->getBody());
         $this->assertEquals((string) $xml->Name,$this->_bucket,'The bucket name in XML response is not valid');
         $this->assertEquals((string) $xml->Name,$this->_bucket,'The bucket name in XML response is not valid');
     }
     }
+    /**
+     * @see ZF-11401
+     */
+    public function testCommonPrefixes()
+    {
+        $this->_amazon->createBucket($this->_bucket);
+        $this->_amazon->putObject($this->_bucket.'/test-folder/test1','test');
+        $this->_amazon->putObject($this->_bucket.'/test-folder/test2-folder/','');
+        $params= array(
+                    'prefix' => 'test-folder/',
+                    'delimiter' => '/'
+                 );
+        $response= $this->_amazon->getObjectsAndPrefixesByBucket($this->_bucket,$params);
+        $this->assertEquals($response['objects'][0],'test-folder/test1');
+        $this->assertEquals($response['prefixes'][0],'test-folder/test2-folder/');
+    }
     public function tearDown()
     public function tearDown()
     {
     {
         unset($this->_amazon->debug);
         unset($this->_amazon->debug);