123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App\Service;
- use OSS\Core\OssException;
- use OSS\OssClient;
- class OssService extends Service
- {
- protected $accessKeyId;
- protected $accessKeySecret;
- protected $endpoint;
- protected $bucket;
- public function __construct()
- {
- $this->accessKeyId = config('aliyun.accessKeyId');
- $this->accessKeySecret = config('aliyun.accessKeySecret');
- $this->endpoint = config('aliyun.endpoint');
- // 存储空间名称
- $this->bucket = config('aliyun.ossBucket');
- }
- /**
- * @desc 添加文件
- * @param $filePath 上传的文件
- * @param $savePath 保存到oss的路径
- */
- public function uploadFile($filePath, $savePath)
- {
- try {
- $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
- $ossClient->uploadFile($this->bucket, $savePath, $filePath);
- } catch (OssException $e) {
- return [false, $e->getMessage()];
- }
- return [true, ''];
- }
- /**
- * @desc 删除文件
- * @param deletePath oss的路径
- */
- public function deleteFile($deletePath)
- {
- try {
- $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
- $ossClient->deleteObject($this->bucket, $deletePath);
- } catch (OssException $e) {
- return [false, $e->getMessage()];
- }
- return [true, ''];
- }
- /**
- * @desc 下载文件
- * @param string $downLoadFile 下载文件地址
- * @param string $saveFile 保存地址
- */
- public function downLoadFile($downLoadFile, $saveFile)
- {
- $localfile = $saveFile;
- $options = array(
- OssClient::OSS_FILE_DOWNLOAD => $localfile
- );
- try {
- $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
- $ossClient->getObject($this->bucket, $downLoadFile, $options);
- } catch (OssException $e) {
- return [false, 'msg' => $e->getMessage()];
- }
- return [true, ''];
- }
- //生成临时文件
- public function getTemporaryUrl($objectKey, $expired = 3500)
- {
- $timeStamp = time();
- // 定义本地文件日志路径
- $localFilePath = storage_path('app/public/oss_file/' . md5($objectKey) . '.txt');
- $directoryPath = dirname($localFilePath);
- // 检查目录是否存在,如果不存在则创建
- if (! is_dir($directoryPath)) {
- // 设置目录权限,可以根据需要更改
- $mode = 0777;
- // 使用递归选项创建目录
- if (!mkdir($directoryPath, $mode, true)) return [false, '目录创建失败'];
- }
- $url = "";
- $timestamp = $timeStamp + $expired;
- if(! file_exists($localFilePath)){
- $url = $this->createUrl($objectKey,$expired);
- $set = [
- 'url' => $url,
- 'timestamp' => $timestamp,
- ];
- file_put_contents($localFilePath, json_encode($set));
- }else{
- $content = file_get_contents($localFilePath);
- $content = json_decode($content,true);
- if($content['timestamp'] < $timeStamp){
- $url = $this->createUrl($objectKey,$expired);
- $set = [
- 'url' => $url,
- 'timestamp' => $timestamp,
- ];
- file_put_contents($localFilePath, json_encode($set));
- }else{
- $url = $content['url'];
- }
- }
- return [true, $url];
- }
- public function createUrl($objectKey, $expiration = 3600){
- // 初始化OSS客户端
- $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
- // 生成临时链接
- $url = $ossClient->signUrl(
- $this->bucket,
- $objectKey,
- $expiration,
- 'GET' // 可选,指定HTTP方法,默认为 GET
- );
- return $url;
- }
- // 阿里云上是否存在图片
- public function isIsset($objectKey){
- // 初始化OSS客户端
- $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
- if ($ossClient->doesObjectExist($this->bucket, $objectKey)) return true;
- return false;
- }
- }
|