From fefba207c9e4b9185779ca3c8110ad7f2494702e Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Wed, 11 Jan 2012 16:55:06 -0600 Subject: [PATCH] You can now set ACLs on containers. --- src/HPCloud/Storage/ObjectStorage.php | 21 +++++++++++++++++-- .../Storage/ObjectStorage/Container.php | 13 ++++++++++++ test/Tests/ObjectStorageTest.php | 21 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/HPCloud/Storage/ObjectStorage.php b/src/HPCloud/Storage/ObjectStorage.php index f65f1a9..4bd68f0 100644 --- a/src/HPCloud/Storage/ObjectStorage.php +++ b/src/HPCloud/Storage/ObjectStorage.php @@ -13,6 +13,7 @@ namespace HPCloud\Storage; use HPCloud\Storage\ObjectStorage\Container; +use HPCloud\Storage\ObjectStorage\ACL; /** * Access to ObjectStorage (Swift). @@ -270,13 +271,29 @@ class ObjectStorage { * * @param string $name * The name of the container. + * @param \HPCloud\Storage\ObjectStorage\ACL $acl + * An access control list object. By default, a container is + * non-public (private). To change this behavior, you can add a + * custom ACL. To make the container publically readable, you can + * use this: `ACL::publicRead()`. * @return boolean * TRUE if the container was created, FALSE if the container was not * created because it already exists. */ - public function createContainer($name) { + public function createContainer($name, ACL $acl = NULL) { $url = $this->url() . '/' . urlencode($name); - $data = $this->req($url, 'PUT', FALSE); + $headers = array( + 'X-Auth-Token' => $this->token(), + ); + + $client = \HPCloud\Transport::instance(); + // Add ACLs to header. + if (!empty($acl)) { + $headers += $acl->headers(); + } + + $data = $client->doRequest($url, 'PUT', $headers); + syslog(LOG_WARNING, print_r($data, TRUE)); $status = $data->status(); diff --git a/src/HPCloud/Storage/ObjectStorage/Container.php b/src/HPCloud/Storage/ObjectStorage/Container.php index 5d2184a..10e39a2 100644 --- a/src/HPCloud/Storage/ObjectStorage/Container.php +++ b/src/HPCloud/Storage/ObjectStorage/Container.php @@ -480,6 +480,19 @@ class Container implements \Countable, \IteratorAggregate { return $this->objectQuery($params, $limit, $marker); } + /** + * Get the URL to this container. + * + * Any container that has been created will have a valid URL. If the + * Container was set to be public (See + * ObjectStorage::createContainer()) will be accessible by this URL. + * + * @return + */ + public function url() { + return $this->url; + } + /** * Perform the HTTP query for a list of objects and de-serialize the * results. diff --git a/test/Tests/ObjectStorageTest.php b/test/Tests/ObjectStorageTest.php index 5d68493..8bff9bc 100644 --- a/test/Tests/ObjectStorageTest.php +++ b/test/Tests/ObjectStorageTest.php @@ -155,4 +155,25 @@ class ObjectStorageTest extends \HPCloud\Tests\TestCase { $store->deleteContainer($testCollection); } + /** + * @depends testCreateContainer + */ + public function testCreateContainerPublic() { + $testCollection = self::$settings['hpcloud.swift.container'] . 'PUBLIC'; + $store = $this->swiftAuth(); + if ($store->hasContainer($testCollection)) { + $store->deleteContainer($testCollection); + } + $ret = $store->createContainer($testCollection, \HPCloud\Storage\ObjectStorage\ACL::publicRead()); + + // Now test that we can get the container contents. Since there is + // no content in the container, we use the format=xml to make sure + // we get some data back. + $container = $store->container($testCollection); + $url = $container->url() . '?format=xml'; + $data = file_get_contents($url); + $this->assertNotEmpty($data, $url); + + $store->deleteContainer($testCollection); + } }