作者 lyh

gx

@@ -5,6 +5,7 @@ namespace App\Http\Controllers\File; @@ -5,6 +5,7 @@ namespace App\Http\Controllers\File;
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Models\File\File; 6 use App\Models\File\File;
7 use App\Models\Project\Project; 7 use App\Models\Project\Project;
  8 +use App\Services\AmazonS3Service;
8 use App\Services\CosService; 9 use App\Services\CosService;
9 use Illuminate\Http\Exceptions\HttpResponseException; 10 use Illuminate\Http\Exceptions\HttpResponseException;
10 use Illuminate\Http\JsonResponse; 11 use Illuminate\Http\JsonResponse;
@@ -125,10 +126,9 @@ class FileController @@ -125,10 +126,9 @@ class FileController
125 $cosService = new CosService(); 126 $cosService = new CosService();
126 $cosService->uploadFile($files,$this->path,$fileName); 127 $cosService->uploadFile($files,$this->path,$fileName);
127 }else{ 128 }else{
128 - $res = $files->move($url,$fileName);  
129 - if ($res === false) {  
130 - return $this->response($files->getError(), Code::USER_ERROR);  
131 - } 129 + //TODO::上传亚马逊
  130 + $amazonS3Service = new AmazonS3Service();
  131 + $amazonS3Service->uploadFiles($files,$this->path,$fileName);
132 } 132 }
133 $this->saveMysql($fileModel,$files->getSize(),$files->getClientOriginalExtension(),$fileName,$hash,$this->upload_location,$files->getMimeType(),$name); 133 $this->saveMysql($fileModel,$files->getSize(),$files->getClientOriginalExtension(),$fileName,$hash,$this->upload_location,$files->getMimeType(),$name);
134 $this->response('资源',Code::SUCCESS,$this->responseData($this->path.'/'.$fileName, $name)); 134 $this->response('资源',Code::SUCCESS,$this->responseData($this->path.'/'.$fileName, $name));
@@ -165,7 +165,9 @@ class FileController @@ -165,7 +165,9 @@ class FileController
165 $cosService = new CosService(); 165 $cosService = new CosService();
166 $cosService->uploadFile($files, $this->path, $fileName); 166 $cosService->uploadFile($files, $this->path, $fileName);
167 } else { 167 } else {
168 - $res = $files->move($url, $fileName); 168 + //TODO::上传亚马逊
  169 + $amazonS3Service = new AmazonS3Service();
  170 + $res = $amazonS3Service->uploadFiles($files,$this->path,$fileName);
169 if ($res === false) { 171 if ($res === false) {
170 return [ 172 return [
171 'message' => $files->getError(), 173 'message' => $files->getError(),
@@ -238,10 +240,9 @@ class FileController @@ -238,10 +240,9 @@ class FileController
238 $cosService = new CosService(); 240 $cosService = new CosService();
239 $cosService->uploadFile($files,$this->path,$fileName); 241 $cosService->uploadFile($files,$this->path,$fileName);
240 }else{ 242 }else{
241 - $res = $files->move($url,$fileName);  
242 - if ($res === false) {  
243 - return $this->response($files->getError(), Code::USER_ERROR);  
244 - } 243 + //TODO::上传亚马逊
  244 + $amazonS3Service = new AmazonS3Service();
  245 + $amazonS3Service->uploadFiles($files,$this->path,$fileName);
245 } 246 }
246 $size = $file->getSize(); 247 $size = $file->getSize();
247 $mime = $file->getMimeType(); 248 $mime = $file->getMimeType();
@@ -7,6 +7,7 @@ use App\Http\Controllers\Controller; @@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
7 use App\Http\Controllers\type; 7 use App\Http\Controllers\type;
8 use App\Models\File\Image as ImageModel; 8 use App\Models\File\Image as ImageModel;
9 use App\Models\Project\Project; 9 use App\Models\Project\Project;
  10 +use App\Services\AmazonS3Service;
10 use App\Services\CosService; 11 use App\Services\CosService;
11 use App\Services\TencentCosService; 12 use App\Services\TencentCosService;
12 use Illuminate\Http\Exceptions\HttpResponseException; 13 use Illuminate\Http\Exceptions\HttpResponseException;
@@ -204,7 +205,9 @@ class ImageController extends Controller @@ -204,7 +205,9 @@ class ImageController extends Controller
204 $cosService = new CosService(); 205 $cosService = new CosService();
205 $cosService->uploadFile($files,$this->path,$fileName); 206 $cosService->uploadFile($files,$this->path,$fileName);
206 }else{ 207 }else{
207 - $files->move($url,$fileName); 208 + //TODO::上传亚马逊
  209 + $amazonS3Service = new AmazonS3Service();
  210 + $amazonS3Service->uploadFiles($files,$this->path,$fileName);
208 } 211 }
209 $this->saveMysql($imageModel,$files->getSize(),$image_type,$fileName,$hash,$this->upload_location,$files->getMimeType(), $name); 212 $this->saveMysql($imageModel,$files->getSize(),$image_type,$fileName,$hash,$this->upload_location,$files->getMimeType(), $name);
210 return $this->response('图片资源',Code::SUCCESS,$this->responseData($this->path.'/'.$fileName, $name)); 213 return $this->response('图片资源',Code::SUCCESS,$this->responseData($this->path.'/'.$fileName, $name));
@@ -282,7 +285,9 @@ class ImageController extends Controller @@ -282,7 +285,9 @@ class ImageController extends Controller
282 $cosService = new CosService(); 285 $cosService = new CosService();
283 $cosService->uploadFile($file,$this->path,$fileName); 286 $cosService->uploadFile($file,$this->path,$fileName);
284 }else{ 287 }else{
285 - $file->move($url,$fileName); 288 + //TODO::上传亚马逊
  289 + $amazonS3Service = new AmazonS3Service();
  290 + $amazonS3Service->uploadFiles($files,$this->path,$fileName);
286 } 291 }
287 //批量存储 292 //批量存储
288 $this->saveMysql($imageModel,$file->getSize(),$image_type,$fileName,$hash,$this->upload_location,$file->getMimeType(),$name); 293 $this->saveMysql($imageModel,$file->getSize(),$image_type,$fileName,$hash,$this->upload_location,$file->getMimeType(),$name);
@@ -15,10 +15,10 @@ class AmazonS3Service @@ -15,10 +15,10 @@ class AmazonS3Service
15 { 15 {
16 // 替换为你自己的 AWS 访问密钥、区域和存储桶名称 16 // 替换为你自己的 AWS 访问密钥、区域和存储桶名称
17 protected $s3; 17 protected $s3;
18 - protected $accessKeyId = '';//key  
19 - protected $secretAccessKey = '';//密匙  
20 - protected $region = '';//地址  
21 - protected $bucket = '';//桶子 18 + protected $accessKeyId = 'AKIAU6YKND7SAWIKBXCZ';//key
  19 + protected $secretAccessKey = 'Hwd2abya/2Icu6NMDo4YrdTqCtir1BeTuUj5kEkB';//密匙
  20 + protected $region = 's3://arn:aws:s3:us-west-2:340934860772:accesspoint/globalso-v6';//地址
  21 + protected $bucket = 'arn:aws:s3:::globalso-v6';//桶子
22 22
23 public function __construct() 23 public function __construct()
24 { 24 {
@@ -40,13 +40,15 @@ class AmazonS3Service @@ -40,13 +40,15 @@ class AmazonS3Service
40 * @method :post 40 * @method :post
41 * @time :2024/1/23 9:20 41 * @time :2024/1/23 9:20
42 */ 42 */
43 - public function uploadFiles($localFilePath, $s3Key) 43 + public function uploadFiles(&$files, $s3Key,$filename ,$binary = false)
44 { 44 {
  45 + $key = $s3Key.'/'.$filename;
  46 + $Body = $binary ? $files : fopen($files->getRealPath(), 'r');
45 try { 47 try {
46 $result = $this->s3->putObject([ 48 $result = $this->s3->putObject([
47 'Bucket' => $this->bucket, 49 'Bucket' => $this->bucket,
48 - 'Key' => $s3Key,  
49 - 'SourceFile' => $localFilePath, 50 + 'Key' => $key,
  51 + 'SourceFile' => $Body,
50 'ACL' => 'public-read', // 设置图片为公共可读,可根据需求修改 52 'ACL' => 'public-read', // 设置图片为公共可读,可根据需求修改
51 ]); 53 ]);
52 return $result['ObjectURL']; 54 return $result['ObjectURL'];