feat : ali

This commit is contained in:
2024-11-06 16:53:45 +08:00
parent 7b6f9e4aa7
commit 8d30a65528
9 changed files with 443 additions and 36 deletions

View File

@@ -10,14 +10,57 @@ declare(strict_types=1);
namespace App\Cron\Oss;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Extend\DateUtil;
use App\Lib\Log;
use App\Model\OssObject;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
#[Crontab(rule: "* * * * *", name: "OssDelByDisableTask", singleton: true , callback: "execute", memo: "这是一个示例的定时任务")]
#[Crontab(rule: "0 5 * * *", name: "OssDelByDisableTask", singleton: true , callback: "execute", memo: "定时删除未使用的oss资源")]
class OssDelByDisableTask
{
public function execute()
/**
* 日志
* @var Log $log
*/
#[Inject]
protected Log $log;
/**
* 缓存
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function execute(): void
{
//todo Write logic
var_dump(date('Y-m-d H:i:s', time()));
$ossObjectModel = new OssObject();
$time = date('Y-m-d H:i:s',time() - DateUtil::DAY);
$list = $ossObjectModel->getOssIdListByIsEnabled($time);
if (empty($list)){
$this->log->notice(__CLASS__.'昨日没有无效的资源');
return;
}
foreach ($list->toArray() as $item) {
$this->redis->lPush(CommonRedisKey::getDeleteOssImgListByOssId(), (string)$item,'system');
}
$this->log->notice(__CLASS__.'异步删除昨日无效图片');
}
}

View File

@@ -10,14 +10,90 @@ declare(strict_types=1);
namespace App\Cron\Oss;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Lib\Log;
use App\Model\OssObject;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
#[Crontab(rule: "* * * * *", name: "OssDelByOssIdTask", singleton: true , callback: "execute", memo: "这是一个示例的定时任务")]
#[Crontab(rule: "* * * * *", name: "OssDelByOssIdTask", singleton: true , callback: "execute", memo: "根据id删除oss的逻辑")]
class OssDelByOssIdTask
{
public function execute()
/**
* 日志
* @var Log $log
*/
#[Inject]
protected Log $log;
/**
* 缓存
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function execute(): void
{
//todo Write logic
var_dump(date('Y-m-d H:i:s', time()));
try {
$key = CommonRedisKey::getDeleteOssImgListByOssId();
$delNum = 0;
$ossIds = [];
for ($i = 1; $i < 50; $i++) {
$one = $this->redis->rPop($key);
if (empty($one)){
continue;
}
$ossIds[] = $one;
}
if (count($ossIds) == 0) {
$this->log->notice(__CLASS__.':success:无根据id删除的列表');
return;
}
$ossObjectModel = new OssObject();
//获取url列表用于删除oss
$urlList = $ossObjectModel->whereIn('id', $ossIds)->pluck('url');
if (empty($urlList)){
$this->log->error(__CLASS__.':删除内容关联失败,无法获取url');
return;
}
if(count($urlList->toArray()) != count($ossIds))
{
$this->log->error(__CLASS__.':删除内容关联失败url数量与id数量不一致');
return;
}
// 删除oss图片
$res = $ossObjectModel->whereIn('id', $ossIds)->delete();
if (!$res) {
$this->log->error(__CLASS__.':删除内容关联失败,删除sql执行失败');
return;
}
//删除oss资源
//把图片地址丢到删除redis队列
foreach ($urlList as $item) {
$this->redis->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $item);
}
$this->log->notice(__CLASS__.':success:删除oss文件个数:' . $delNum);
}catch (\Exception $e){
$this->log->error(__CLASS__.':'.$e->getMessage());
return;
}
}
}

View File

@@ -10,14 +10,66 @@ 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: "这是一个示例的定时任务")]
#[Crontab(rule: "* * * * *", name: "OssDelByUrlTask", singleton: true , callback: "execute", memo: "根据url删除oss的逻辑")]
class OssDelByUrlTask
{
public function execute()
/**
* 日志
* @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
{
//todo Write logic
var_dump(date('Y-m-d H:i:s', time()));
$key = CommonRedisKey::getDeleteOssImgListByUrl();
// 阿里云oss上传
$OssClient = new OssClient(
config('ali.access_key_id'),
config('ali.access_key_secret'),
config('ali.intranet_endpoint')
);
$bucket = config('ali.bucket');
$delNum = 0;
for ($i = 1; $i < 50; $i++) {
$url = $this->redis->rPop($key);
if (!empty($url)) {
$delNum++;
$OssClient->deleteObject($bucket, $url);
}
}
$this->log->notice(__CLASS__.':success:删除oss文件个数:' . $delNum);
}
}