From 4872bae09479b8637335df4b61c246d4cb0aa245 Mon Sep 17 00:00:00 2001 From: David Ducatel Date: Fri, 15 Jan 2016 11:10:44 +0100 Subject: [PATCH 1/3] Add delete object method in the container of ObjectStore. --- doc/services/object-store/objects.rst | 15 ++++++-- .../ObjectStore/Resource/Container.php | 22 ++++++++++++ .../delete-object-without-download.php | 36 +++++++++++++++++++ .../ObjectStore/Resource/ContainerTest.php | 26 ++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 samples/ObjectStore/delete-object-without-download.php diff --git a/doc/services/object-store/objects.rst b/doc/services/object-store/objects.rst index 3bc221dcc..b9928e3fb 100644 --- a/doc/services/object-store/objects.rst +++ b/doc/services/object-store/objects.rst @@ -495,9 +495,20 @@ filenames inside the archive. `Get the executable PHP script for this example `_ - Delete object -------------- +--------------------------------- + +.. code-block:: php + + /** @var bool $deleted */ + $deleted = $container->deleteObject('{objectName}'); + + +`Get the executable PHP script for this example `_ + + +Delete already downloaded object +--------------------------------- .. code-block:: php diff --git a/lib/OpenCloud/ObjectStore/Resource/Container.php b/lib/OpenCloud/ObjectStore/Resource/Container.php index ed47a4d35..37826b78e 100644 --- a/lib/OpenCloud/ObjectStore/Resource/Container.php +++ b/lib/OpenCloud/ObjectStore/Resource/Container.php @@ -235,6 +235,28 @@ public function deleteAllObjects() return $this->getService()->batchDelete($paths); } + /** + * Delete an object from the API. + * + * @param string $name The name of object you want to delete + * @return True if object was delete with success. False if an error occurred + * @throws ObjectNotFoundException When the file you want to delete is not found + */ + public function deleteObject($name) + { + try { + $response = $this->getClient() + ->delete($this->getUrl($name)) + ->send(); + return ($response->getStatusCode() == 204); + } catch (BadResponseException $e) { + if ($e->getResponse()->getStatusCode() == 404) { + throw ObjectNotFoundException::factory($name, $e); + } + } + return false; + } + /** * Creates a Collection of objects in the container * diff --git a/samples/ObjectStore/delete-object-without-download.php b/samples/ObjectStore/delete-object-without-download.php new file mode 100644 index 000000000..bbed09c2e --- /dev/null +++ b/samples/ObjectStore/delete-object-without-download.php @@ -0,0 +1,36 @@ + '{username}', + 'apiKey' => '{apiKey}', +)); + +// 2. Obtain an Object Store service object from the client. +$objectStoreService = $client->objectStoreService(null, '{region}'); + +// 3. Get container. +$container = $objectStoreService->getContainer('{containerName}'); + +// 4. Delete object. +$container->deleteObject('{objectName}'); diff --git a/tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php b/tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php index c5b191249..c79b35634 100644 --- a/tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php +++ b/tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php @@ -118,6 +118,32 @@ public function test_Delete_NonEmpty_Container() $container->delete(); } + public function test_Delete_Object() + { + $container = $this->container; + $this->addMockSubscriber($this->makeResponse('[]', 204)); + $response = $container->deleteObject("someObject"); + $this->assertTrue($response); + } + + public function test_Delete_Object_With_Failure() + { + $container = $this->container; + $this->addMockSubscriber($this->makeResponse('[]', 500)); + $response = $container->deleteObject("someObject"); + $this->assertFalse($response); + } + + /** + * @expectedException \OpenCloud\ObjectStore\Exception\ObjectNotFoundException + */ + public function test_Delete_NonExisting_Object() + { + $container = $this->container; + $this->addMockSubscriber($this->makeResponse('[]', 404)); + $container->deleteObject("someObject"); + } + public function test_Object_List() { $container = $this->container; From 87a0eb5655754a3a1626fb81db315e1d488c9c33 Mon Sep 17 00:00:00 2001 From: David Ducatel Date: Thu, 28 Jan 2016 11:05:50 +0100 Subject: [PATCH 2/3] Remove catching exception --- .../ObjectStore/Resource/Container.php | 17 ++++------------- .../ObjectStore/Resource/ContainerTest.php | 11 ++++++----- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/OpenCloud/ObjectStore/Resource/Container.php b/lib/OpenCloud/ObjectStore/Resource/Container.php index 37826b78e..94f33b32e 100644 --- a/lib/OpenCloud/ObjectStore/Resource/Container.php +++ b/lib/OpenCloud/ObjectStore/Resource/Container.php @@ -239,22 +239,13 @@ public function deleteAllObjects() * Delete an object from the API. * * @param string $name The name of object you want to delete - * @return True if object was delete with success. False if an error occurred - * @throws ObjectNotFoundException When the file you want to delete is not found + * @throws \Guzzle\Http\Exception\BadResponseException When an error occurred */ public function deleteObject($name) { - try { - $response = $this->getClient() - ->delete($this->getUrl($name)) - ->send(); - return ($response->getStatusCode() == 204); - } catch (BadResponseException $e) { - if ($e->getResponse()->getStatusCode() == 404) { - throw ObjectNotFoundException::factory($name, $e); - } - } - return false; + $response = $this->getClient() + ->delete($this->getUrl($name)) + ->send(); } /** diff --git a/tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php b/tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php index c79b35634..2b02418a0 100644 --- a/tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php +++ b/tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php @@ -122,20 +122,21 @@ public function test_Delete_Object() { $container = $this->container; $this->addMockSubscriber($this->makeResponse('[]', 204)); - $response = $container->deleteObject("someObject"); - $this->assertTrue($response); + $container->deleteObject("someObject"); } + /** + * @expectedException \Guzzle\Http\Exception\BadResponseException + */ public function test_Delete_Object_With_Failure() { $container = $this->container; $this->addMockSubscriber($this->makeResponse('[]', 500)); - $response = $container->deleteObject("someObject"); - $this->assertFalse($response); + $container->deleteObject("someObject"); } /** - * @expectedException \OpenCloud\ObjectStore\Exception\ObjectNotFoundException + * @expectedException \Guzzle\Http\Exception\BadResponseException */ public function test_Delete_NonExisting_Object() { From 90358518a0ff057ac6c8ba0217be6cbdd634922e Mon Sep 17 00:00:00 2001 From: David Ducatel Date: Fri, 29 Jan 2016 11:25:56 +0100 Subject: [PATCH 3/3] Remove unused variables --- doc/services/object-store/objects.rst | 3 +-- lib/OpenCloud/ObjectStore/Resource/Container.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/doc/services/object-store/objects.rst b/doc/services/object-store/objects.rst index b9928e3fb..6d7362246 100644 --- a/doc/services/object-store/objects.rst +++ b/doc/services/object-store/objects.rst @@ -500,8 +500,7 @@ Delete object .. code-block:: php - /** @var bool $deleted */ - $deleted = $container->deleteObject('{objectName}'); + $container->deleteObject('{objectName}'); `Get the executable PHP script for this example `_ diff --git a/lib/OpenCloud/ObjectStore/Resource/Container.php b/lib/OpenCloud/ObjectStore/Resource/Container.php index 94f33b32e..474d9134b 100644 --- a/lib/OpenCloud/ObjectStore/Resource/Container.php +++ b/lib/OpenCloud/ObjectStore/Resource/Container.php @@ -243,7 +243,7 @@ public function deleteAllObjects() */ public function deleteObject($name) { - $response = $this->getClient() + $this->getClient() ->delete($this->getUrl($name)) ->send(); }