在网上找了半天的OBS批量删除 结果都是单删除 自己琢磨着弄出来了哈哈 分享出来吧 不用谢我! 可能还有地方需要优化,大佬勿喷!
下载OBS的SDK
然后配置你的秘钥什么的
<?php
namespace app\kaifa\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\kaifa\controller\Common;
use app\api\controller\Share;
use Obs\ObsClient;
use Obs\ObsException;
use think\Env;
/**
* @autor long
* @Date 2022/3/29 10:25
* @package app\kaifa\controller
*/
class Ggck extends Common
{
public function _initialize()
{
parent::initialize();
}
//华为云OBS批量删除对象
public function delimg($link)
{
require EXTEND_PATH . "Obs/ObsClient.php";
require EXTEND_PATH . "GuzzleHttp/functions_include.php";
require EXTEND_PATH . "GuzzleHttp/Psr7/functions_include.php";
require EXTEND_PATH . "GuzzleHttp/Promise/functions_include.php";
$dat = $this->hwobs();
$res = [];
foreach ($link as $k => $v) {
$v['Key'] = mb_substr($v['imgurl'], 70);
$res[] = $v;
}
//华为云OBS配置参数
$obsClient = ObsClient::factory(array(
'key' => $dat['accessKeyId'], //ak
'secret' => $dat['accessKeySecret'],//sk
'endpoint' => $dat['endpoint'],//endpoint
));
//华为云OBS批量删除
$resp = $obsClient->deleteObjects([
//桶
'Bucket' => $dat['bucket'],
// 设置为verbose模式
'Quiet' => false,
//待删除对象列表 数组形式
'Objects' => $res,
]);
//后续可操作 数据库判断成功
if ($res) {
return json_encode(['code' => 200, 'message' => '删除成功']);
} else {
return json_encode(['code' => 118, 'message' => '删除失败']);
}
}
}
待删除对象列表数据结构实例
'Objects' => [
[
'Key' => 'objectname1',
],
[
'Key' => 'objectname2',
]
]