You can now set ACLs on containers.

This commit is contained in:
Matt Butcher
2012-01-11 16:55:06 -06:00
parent aa5bfaeecd
commit fefba207c9
3 changed files with 53 additions and 2 deletions

View File

@@ -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();

View File

@@ -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.

View File

@@ -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);
}
}