diff --git a/doc/services/object-store/objects.rst b/doc/services/object-store/objects.rst index 3bc221dcc..6d7362246 100644 --- a/doc/services/object-store/objects.rst +++ b/doc/services/object-store/objects.rst @@ -495,9 +495,19 @@ filenames inside the archive. `Get the executable PHP script for this example `_ - Delete object -------------- +--------------------------------- + +.. code-block:: php + + $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..474d9134b 100644 --- a/lib/OpenCloud/ObjectStore/Resource/Container.php +++ b/lib/OpenCloud/ObjectStore/Resource/Container.php @@ -235,6 +235,19 @@ 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 + * @throws \Guzzle\Http\Exception\BadResponseException When an error occurred + */ + public function deleteObject($name) + { + $this->getClient() + ->delete($this->getUrl($name)) + ->send(); + } + /** * 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..2b02418a0 100644 --- a/tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php +++ b/tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php @@ -118,6 +118,33 @@ public function test_Delete_NonEmpty_Container() $container->delete(); } + public function test_Delete_Object() + { + $container = $this->container; + $this->addMockSubscriber($this->makeResponse('[]', 204)); + $container->deleteObject("someObject"); + } + + /** + * @expectedException \Guzzle\Http\Exception\BadResponseException + */ + public function test_Delete_Object_With_Failure() + { + $container = $this->container; + $this->addMockSubscriber($this->makeResponse('[]', 500)); + $container->deleteObject("someObject"); + } + + /** + * @expectedException \Guzzle\Http\Exception\BadResponseException + */ + 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;