Browse Source

修改上传文件接口,接入火山tos

lh 1 ngày trước cách đây
mục cha
commit
4b6e8e4df0
3 tập tin đã thay đổi với 65 bổ sung11 xóa
  1. 44 2
      app/Libs/Helpers.php
  2. 19 8
      app/Services/Book/BookService.php
  3. 2 1
      composer.json

+ 44 - 2
app/Libs/Helpers.php

@@ -8,6 +8,11 @@ use Monolog\Logger;
 use OSS\OssClient;
 use OSS\Core\OssException;
 
+use Tos\TosClient;
+use Tos\Exception\TosClientException;
+use Tos\Exception\TosServerException;
+use Tos\Model\PutObjectInput;
+
 /**
  * 截取字符串
  *
@@ -256,9 +261,46 @@ function randStr($num = 5)
     return substr(str_shuffle($str), random_int(0, strlen($str) - 11), $num);
 }
 
+/**
+ * 上传文件(阿里云oss)
+ *
+ * @param $prefix   文件夹前缀
+ * @param $file     文件二进制
+ * @param $filename 文件名(不带后缀)
+ * @return mixed
+ */
+function uploadFileByTos($prefix, $file, $filename='')
+{
+    if (!$filename) $filename = randStr(10) . '.' . $file->getClientOriginalExtension();
+    else $filename = $filename . '.' . $file->getClientOriginalExtension();
+
+    try {
+        $client = new TosClient([
+            'region'    => env('VOLC_REGION'),
+            'endpoint'  => env('VOLC_END_POINT'),
+            'ak'        => env('VOLC_AK'),
+            'sk'        => env('VOLC_SK'),
+        ]);
+        
+        $stream = fopen($file->getRealPath(), 'r');
+
+        $input = new PutObjectInput(env('VOLC_BUCKET'),  "$prefix/$filename", $stream);
+        $output = $client->putObject($input);
+        if ($output->getRequestId()) {
+            return "https://".env('VOLC_BUCKET').'.'.env('VOLC_END_POINT')."/$prefix/$filename";
+        }
+    } catch (TosClientException $ex) {
+        dLog('exception')->info('saveToTos', [$ex->getMessage()]);
+    } catch (TosServerException $ex) {
+        dLog('exception')->info('saveToTos', [$ex->getRequestId(), $ex->getStatusCode(), $ex->getErrorCode()]);
+    }
+
+    return '';
+}
+
 
 /**
- * 上传文件
+ * 上传文件(阿里云oss)
  *
  * @param $prefix   文件夹前缀
  * @param $file     文件二进制
@@ -302,7 +344,7 @@ function uploadFile($prefix, $file, $filename='')
 }
 
 /**
- * 上传文件流
+ * 上传文件流(阿里云oss)
  *
  * @param $prefix   文件夹前缀
  * @param $file     文件二进制

+ 19 - 8
app/Services/Book/BookService.php

@@ -322,8 +322,11 @@ class BookService
         if (!$audio_effect_name) Utils::throwError('20003:请输入音频效果名称');
         if (DB::table('mp_audio_effects')->where('audio_effect_name', $audio_effect_name)->exists()) Utils::throwError('20003:音效名称已存在');
         
-        // 上传文件到oss
-        $audio_effect_url = uploadFile('audio_effects', $file, $audio_effect_name);
+        // // 上传文件到oss
+        // $audio_effect_url = uploadFile('audio_effects', $file, $audio_effect_name);
+        // 上传文件到tos
+        $audio_effect_url = uploadFileByTos('effects', $file, $audio_effect_name);
+
         if (!$audio_effect_url) Utils::throwError('20003:上传音频文件失败');
 
         $boolen = DB::table('mp_audio_effects')->insert([
@@ -368,8 +371,11 @@ class BookService
             if (!$audio_effect_name) Utils::throwError('20003:请输入音频效果名称');
             if (DB::table('mp_audio_effects')->where('audio_effect_name', $audio_effect_name)->exists()) Utils::throwError('20003:音效名称已存在');
             
-            // 上传文件到oss
-            $audio_effect_url = uploadFile('audio_effects', $file, $audio_effect_name);
+            // // 上传文件到oss
+            // $audio_effect_url = uploadFile('audio_effects', $file, $audio_effect_name);
+            // 上传文件到tos
+            $audio_effect_url = uploadFileByTos('effects', $file, $audio_effect_name);
+
             if (!$audio_effect_url) Utils::throwError('20003:上传音频文件失败');
             $update_data['audio_effect_url'] = $audio_effect_url;
         }
@@ -416,8 +422,10 @@ class BookService
         if (!$bgm_name) Utils::throwError('20003:请输入音频效果名称');
         if (DB::table('mp_bgms')->where('bgm_name', $bgm_name)->exists()) Utils::throwError('20003:bgm名称已存在');
         
-        // 上传文件到oss
-        $bgm_url = uploadFile('bgms', $file, $bgm_name);
+        // // 上传文件到oss
+        // $bgm_url = uploadFile('bgms', $file, $bgm_name);
+        // 上传文件到tos
+        $bgm_url = uploadFileByTos('bgm', $file, $bgm_name);
         if (!$bgm_url) Utils::throwError('20003:上传音频文件失败');
 
         $boolen = DB::table('mp_bgms')->insert([
@@ -461,8 +469,11 @@ class BookService
             if (!$bgm_name) Utils::throwError('20003:请输入音频效果名称');
             if (DB::table('mp_bgms')->where('bgm_name', $bgm_name)->exists()) Utils::throwError('20003:bgm名称已存在');
             
-            // 上传文件到oss
-            $bgm_url = uploadFile('bgms', $file, $bgm_name);
+            // // 上传文件到oss
+            // $bgm_url = uploadFile('bgms', $file, $bgm_name);
+            // 上传文件到tos
+            $bgm_url = uploadFileByTos('bgm', $file, $bgm_name);
+            
             if (!$bgm_url) Utils::throwError('20003:上传音频文件失败');
             $update_data['bgm_url'] = $bgm_url;
         }

+ 2 - 1
composer.json

@@ -23,7 +23,8 @@
 		"pimple/pimple": "^3.0",
 		"predis/predis": "^2.1",
 		"symfony/cache": "^5.0",
-		"vinkla/hashids": "^9.1"
+		"vinkla/hashids": "^9.1",
+		"volcengine/ve-tos-php-sdk": "^2.1"
 	},
 	"require-dev" : {
 		"facade/ignition" : "^2.5",