作者 lyh

gx

... ... @@ -5,6 +5,7 @@ namespace App\Http\Controllers\File;
use App\Enums\Common\Code;
use App\Models\File\File;
use App\Models\Project\Project;
use App\Services\AmazonS3Service;
use App\Services\CosService;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
... ... @@ -125,10 +126,9 @@ class FileController
$cosService = new CosService();
$cosService->uploadFile($files,$this->path,$fileName);
}else{
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
//TODO::上传亚马逊
$amazonS3Service = new AmazonS3Service();
$amazonS3Service->uploadFiles($files,$this->path,$fileName);
}
$this->saveMysql($fileModel,$files->getSize(),$files->getClientOriginalExtension(),$fileName,$hash,$this->upload_location,$files->getMimeType(),$name);
$this->response('资源',Code::SUCCESS,$this->responseData($this->path.'/'.$fileName, $name));
... ... @@ -165,7 +165,9 @@ class FileController
$cosService = new CosService();
$cosService->uploadFile($files, $this->path, $fileName);
} else {
$res = $files->move($url, $fileName);
//TODO::上传亚马逊
$amazonS3Service = new AmazonS3Service();
$res = $amazonS3Service->uploadFiles($files,$this->path,$fileName);
if ($res === false) {
return [
'message' => $files->getError(),
... ... @@ -238,10 +240,9 @@ class FileController
$cosService = new CosService();
$cosService->uploadFile($files,$this->path,$fileName);
}else{
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
//TODO::上传亚马逊
$amazonS3Service = new AmazonS3Service();
$amazonS3Service->uploadFiles($files,$this->path,$fileName);
}
$size = $file->getSize();
$mime = $file->getMimeType();
... ...
... ... @@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
use App\Http\Controllers\type;
use App\Models\File\Image as ImageModel;
use App\Models\Project\Project;
use App\Services\AmazonS3Service;
use App\Services\CosService;
use App\Services\TencentCosService;
use Illuminate\Http\Exceptions\HttpResponseException;
... ... @@ -204,7 +205,9 @@ class ImageController extends Controller
$cosService = new CosService();
$cosService->uploadFile($files,$this->path,$fileName);
}else{
$files->move($url,$fileName);
//TODO::上传亚马逊
$amazonS3Service = new AmazonS3Service();
$amazonS3Service->uploadFiles($files,$this->path,$fileName);
}
$this->saveMysql($imageModel,$files->getSize(),$image_type,$fileName,$hash,$this->upload_location,$files->getMimeType(), $name);
return $this->response('图片资源',Code::SUCCESS,$this->responseData($this->path.'/'.$fileName, $name));
... ... @@ -282,7 +285,9 @@ class ImageController extends Controller
$cosService = new CosService();
$cosService->uploadFile($file,$this->path,$fileName);
}else{
$file->move($url,$fileName);
//TODO::上传亚马逊
$amazonS3Service = new AmazonS3Service();
$amazonS3Service->uploadFiles($files,$this->path,$fileName);
}
//批量存储
$this->saveMysql($imageModel,$file->getSize(),$image_type,$fileName,$hash,$this->upload_location,$file->getMimeType(),$name);
... ...
... ... @@ -15,10 +15,10 @@ class AmazonS3Service
{
// 替换为你自己的 AWS 访问密钥、区域和存储桶名称
protected $s3;
protected $accessKeyId = '';//key
protected $secretAccessKey = '';//密匙
protected $region = '';//地址
protected $bucket = '';//桶子
protected $accessKeyId = 'AKIAU6YKND7SAWIKBXCZ';//key
protected $secretAccessKey = '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';//密匙
protected $region = 's3://arn:aws:s3:us-west-2:340934860772:accesspoint/globalso-v6';//地址
protected $bucket = 'arn:aws:s3:::globalso-v6';//桶子
public function __construct()
{
... ... @@ -40,13 +40,15 @@ class AmazonS3Service
* @method :post
* @time :2024/1/23 9:20
*/
public function uploadFiles($localFilePath, $s3Key)
public function uploadFiles(&$files, $s3Key,$filename ,$binary = false)
{
$key = $s3Key.'/'.$filename;
$Body = $binary ? $files : fopen($files->getRealPath(), 'r');
try {
$result = $this->s3->putObject([
'Bucket' => $this->bucket,
'Key' => $s3Key,
'SourceFile' => $localFilePath,
'Key' => $key,
'SourceFile' => $Body,
'ACL' => 'public-read', // 设置图片为公共可读,可根据需求修改
]);
return $result['ObjectURL'];
... ...