Skip to content

Commit d6b71fe

Browse files
author
Jamie Hannaford
committed
Merge pull request #651 from Ducatel/working
Add delete object method in the container of ObjectStore.
2 parents dad7eaf + 9035851 commit d6b71fe

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

doc/services/object-store/objects.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,19 @@ filenames inside the archive.
495495

496496
`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/ObjectStore/auto-extract-archive-files.php>`_
497497

498-
499498
Delete object
500-
-------------
499+
---------------------------------
500+
501+
.. code-block:: php
502+
503+
$container->deleteObject('{objectName}');
504+
505+
506+
`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/ObjectStore/delete-object-without-download.php>`_
507+
508+
509+
Delete already downloaded object
510+
---------------------------------
501511

502512
.. code-block:: php
503513

lib/OpenCloud/ObjectStore/Resource/Container.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,19 @@ public function deleteAllObjects()
235235
return $this->getService()->batchDelete($paths);
236236
}
237237

238+
/**
239+
* Delete an object from the API.
240+
*
241+
* @param string $name The name of object you want to delete
242+
* @throws \Guzzle\Http\Exception\BadResponseException When an error occurred
243+
*/
244+
public function deleteObject($name)
245+
{
246+
$this->getClient()
247+
->delete($this->getUrl($name))
248+
->send();
249+
}
250+
238251
/**
239252
* Creates a Collection of objects in the container
240253
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright 2012-2014 Rackspace US, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
require dirname(__DIR__) . '/../vendor/autoload.php';
19+
20+
use OpenCloud\Rackspace;
21+
22+
// 1. Instantiate a Rackspace client. You can replace {authUrl} with
23+
// Rackspace::US_IDENTITY_ENDPOINT or similar
24+
$client = new Rackspace('{authUrl}', array(
25+
'username' => '{username}',
26+
'apiKey' => '{apiKey}',
27+
));
28+
29+
// 2. Obtain an Object Store service object from the client.
30+
$objectStoreService = $client->objectStoreService(null, '{region}');
31+
32+
// 3. Get container.
33+
$container = $objectStoreService->getContainer('{containerName}');
34+
35+
// 4. Delete object.
36+
$container->deleteObject('{objectName}');

tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,33 @@ public function test_Delete_NonEmpty_Container()
118118
$container->delete();
119119
}
120120

121+
public function test_Delete_Object()
122+
{
123+
$container = $this->container;
124+
$this->addMockSubscriber($this->makeResponse('[]', 204));
125+
$container->deleteObject("someObject");
126+
}
127+
128+
/**
129+
* @expectedException \Guzzle\Http\Exception\BadResponseException
130+
*/
131+
public function test_Delete_Object_With_Failure()
132+
{
133+
$container = $this->container;
134+
$this->addMockSubscriber($this->makeResponse('[]', 500));
135+
$container->deleteObject("someObject");
136+
}
137+
138+
/**
139+
* @expectedException \Guzzle\Http\Exception\BadResponseException
140+
*/
141+
public function test_Delete_NonExisting_Object()
142+
{
143+
$container = $this->container;
144+
$this->addMockSubscriber($this->makeResponse('[]', 404));
145+
$container->deleteObject("someObject");
146+
}
147+
121148
public function test_Object_List()
122149
{
123150
$container = $this->container;

0 commit comments

Comments
 (0)