Skip to content

Commit 4872bae

Browse files
committed
Add delete object method in the container of ObjectStore.
1 parent 27deb5d commit 4872bae

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

doc/services/object-store/objects.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,20 @@ 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+
/** @var bool $deleted */
504+
$deleted = $container->deleteObject('{objectName}');
505+
506+
507+
`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/ObjectStore/delete-object-without-download.php>`_
508+
509+
510+
Delete already downloaded object
511+
---------------------------------
501512

502513
.. code-block:: php
503514

lib/OpenCloud/ObjectStore/Resource/Container.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,28 @@ 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+
* @return True if object was delete with success. False if an error occurred
243+
* @throws ObjectNotFoundException When the file you want to delete is not found
244+
*/
245+
public function deleteObject($name)
246+
{
247+
try {
248+
$response = $this->getClient()
249+
->delete($this->getUrl($name))
250+
->send();
251+
return ($response->getStatusCode() == 204);
252+
} catch (BadResponseException $e) {
253+
if ($e->getResponse()->getStatusCode() == 404) {
254+
throw ObjectNotFoundException::factory($name, $e);
255+
}
256+
}
257+
return false;
258+
}
259+
238260
/**
239261
* Creates a Collection of objects in the container
240262
*
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,32 @@ 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+
$response = $container->deleteObject("someObject");
126+
$this->assertTrue($response);
127+
}
128+
129+
public function test_Delete_Object_With_Failure()
130+
{
131+
$container = $this->container;
132+
$this->addMockSubscriber($this->makeResponse('[]', 500));
133+
$response = $container->deleteObject("someObject");
134+
$this->assertFalse($response);
135+
}
136+
137+
/**
138+
* @expectedException \OpenCloud\ObjectStore\Exception\ObjectNotFoundException
139+
*/
140+
public function test_Delete_NonExisting_Object()
141+
{
142+
$container = $this->container;
143+
$this->addMockSubscriber($this->makeResponse('[]', 404));
144+
$container->deleteObject("someObject");
145+
}
146+
121147
public function test_Object_List()
122148
{
123149
$container = $this->container;

0 commit comments

Comments
 (0)