Files
hyperf_service/app/Cron/Oss/OssDelByUrlTask.php
2024-11-12 11:14:48 +08:00

76 lines
1.8 KiB
PHP

<?php
/**
* This crontab file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Cron\Oss;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Lib\Log;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;
use OSS\Core\OssException;
use OSS\Http\RequestCore_Exception;
use OSS\OssClient;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
use function Hyperf\Config\config;
#[Crontab(rule: "* * * * *", name: "OssDelByUrlTask", singleton: true , callback: "execute", memo: "根据url删除oss的逻辑")]
class OssDelByUrlTask
{
/**
* 日志
* @var Log $log
*/
#[Inject]
protected Log $log;
/**
* 缓存
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @return void
* @throws OssException
* @throws RequestCore_Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function execute(): void
{
$key = CommonRedisKey::getDeleteOssImgListByUrl();
// 阿里云oss上传
$OssClient = new OssClient(
config('ali.access_key_id'),
config('ali.access_key_secret'),
config('ali.oss_endpoint')
);
$bucket = config('ali.bucket');
$delNum = 0;
for ($i = 1; $i < 50; $i++) {
$url = $this->redis->rPop($key,'system');
if (!empty($url)) {
$delNum++;
$OssClient->deleteObject($bucket, $url);
}
}
if ($delNum == 0) return;
$this->log->notice(__CLASS__.':success:删除oss文件个数:' . $delNum);
}
}