OssService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Cache;
  4. use OSS\Core\OssException;
  5. use OSS\OssClient;
  6. class OssService extends Service
  7. {
  8. protected $accessKeyId;
  9. protected $accessKeySecret;
  10. protected $endpoint;
  11. protected $bucket;
  12. public function __construct()
  13. {
  14. $this->accessKeyId = config('aliyun.accessKeyId');
  15. $this->accessKeySecret = config('aliyun.accessKeySecret');
  16. $this->endpoint = config('aliyun.endpoint');
  17. // 存储空间名称
  18. $this->bucket = config('aliyun.ossBucket');
  19. }
  20. /**
  21. * @desc 添加文件
  22. * @param $filePath 上传的文件
  23. * @param $savePath 保存到oss的路径
  24. */
  25. public function uploadFile($filePath, $savePath)
  26. {
  27. try {
  28. $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
  29. $ossClient->uploadFile($this->bucket, $savePath, $filePath);
  30. } catch (OssException $e) {
  31. return [false, $e->getMessage()];
  32. }
  33. return [true, ''];
  34. }
  35. /**
  36. * @desc 删除文件
  37. * @param deletePath oss的路径
  38. */
  39. public function deleteFile($deletePath)
  40. {
  41. try {
  42. $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
  43. $ossClient->deleteObject($this->bucket, $deletePath);
  44. } catch (OssException $e) {
  45. return [false, $e->getMessage()];
  46. }
  47. return [true, ''];
  48. }
  49. /**
  50. * @desc 下载文件
  51. * @param string $downLoadFile 下载文件地址
  52. * @param string $saveFile 保存地址
  53. */
  54. public function downLoadFile($downLoadFile, $saveFile)
  55. {
  56. $localfile = $saveFile;
  57. $options = array(
  58. OssClient::OSS_FILE_DOWNLOAD => $localfile
  59. );
  60. try {
  61. $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
  62. $ossClient->getObject($this->bucket, $downLoadFile, $options);
  63. } catch (OssException $e) {
  64. return [false, 'msg' => $e->getMessage()];
  65. }
  66. return [true, ''];
  67. }
  68. //生成临时文件
  69. public function getTemporaryUrl1($objectKey, $expired = 3500)
  70. {
  71. $timeStamp = time();
  72. // 定义本地文件日志路径
  73. $localFilePath = storage_path('app/public/oss_file/' . md5($objectKey) . '.txt');
  74. $directoryPath = dirname($localFilePath);
  75. // 检查目录是否存在,如果不存在则创建
  76. if (! is_dir($directoryPath)) {
  77. // 设置目录权限,可以根据需要更改
  78. $mode = 0777;
  79. // 使用递归选项创建目录
  80. if (!mkdir($directoryPath, $mode, true)) return [false, '目录创建失败'];
  81. }
  82. $url = "";
  83. $timestamp = $timeStamp + $expired;
  84. if(! file_exists($localFilePath)){
  85. $url = $this->createUrl($objectKey,$expired);
  86. $set = [
  87. 'url' => $url,
  88. 'timestamp' => $timestamp,
  89. ];
  90. file_put_contents($localFilePath, json_encode($set));
  91. }else{
  92. $content = file_get_contents($localFilePath);
  93. $content = json_decode($content,true);
  94. if($content['timestamp'] < $timeStamp){
  95. $url = $this->createUrl($objectKey,$expired);
  96. $set = [
  97. 'url' => $url,
  98. 'timestamp' => $timestamp,
  99. ];
  100. file_put_contents($localFilePath, json_encode($set));
  101. }else{
  102. $url = $content['url'];
  103. }
  104. }
  105. return [true, $url];
  106. }
  107. public function getTemporaryUrl($objectKey, $expired = 3500)
  108. {
  109. $projectName = env('APP_NAME');
  110. $cacheKey = 'oss_url_' . $projectName. '_' . md5($objectKey);
  111. $url = Cache::remember($cacheKey, $expired - 60, function () use ($objectKey, $expired) {
  112. return $this->createUrl($objectKey, $expired);
  113. });
  114. return [true, $url];
  115. }
  116. public function createUrl($objectKey, $expiration = 3600){
  117. // 初始化OSS客户端
  118. $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
  119. // 生成临时链接
  120. $url = $ossClient->signUrl(
  121. $this->bucket,
  122. $objectKey,
  123. $expiration,
  124. 'GET' // 可选,指定HTTP方法,默认为 GET
  125. );
  126. return $url;
  127. }
  128. // 阿里云上是否存在图片
  129. public function isIsset($objectKey){
  130. // 初始化OSS客户端
  131. $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
  132. if ($ossClient->doesObjectExist($this->bucket, $objectKey)) return true;
  133. return false;
  134. }
  135. }