feat : ali
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user