本文介绍删除单个对象和批量删除对象的示例代码。
您可以通过 TosClient->deleteObject 和 TosClient->deleteMultiObjects 分别实现删除单个对象和批量删除对象,示例代码如下:
<?php // 假设使用 composer 安装 require_once __DIR__ . '/vendor/autoload.php'; use Tos\TosClient; use Tos\Exception\TosClientException; use Tos\Exception\TosServerException; use Tos\Model\DeleteObjectInput; use Tos\Model\DeleteMultiObjectsInput; use Tos\Model\ObjectTobeDeleted; try { $client = new TosClient([ 'region' => 'your region', 'endpoint' => 'your endpoint', // 从环境变量中获取访问密钥 'ak' => getenv('TOS_ACCESS_KEY'), 'sk' => getenv('TOS_SECRET_KEY'), ]); // 删除单个对象 $input = new DeleteObjectInput('bucket-test', 'key-test'); $output = $client->deleteObject($input); echo $output->getRequestId() . PHP_EOL; // 批量删除对象 $objects = []; $objects[] = new ObjectTobeDeleted('key-test1'); $objects[] = new ObjectTobeDeleted('key-test2'); $input = new DeleteMultiObjectsInput('bucket-test'); $input->setObjects($objects); $output = $client->deleteMultiObjects($input); echo $output->getRequestId() . PHP_EOL; // 获取删除成功结果 echo 'delete succeed:' . PHP_EOL; foreach ($output->getDeleted() as $deleted) { echo $deleted->getKey() . PHP_EOL; } // 获取删除失败结果 echo 'delete error:' . PHP_EOL; foreach ($output->getError() as $error) { echo $error->getKey() . PHP_EOL; echo $error->getCode() . PHP_EOL; } } catch (TosClientException $ex) { echo $ex->getMessage() . PHP_EOL; } catch (TosServerException $ex) { echo $ex->getRequestId() . PHP_EOL; echo $ex->getStatusCode() . PHP_EOL; echo $ex->getErrorCode() . PHP_EOL; }