OssService.php 3.9 KB

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