request->file('file'); $param = $this->request->param(); $rootPath = $param['path'] ?? ''; $filename = $param['name'] ?? ''; // 将文件存储在本地 $name = Filesystem::disk('local')->putFile($rootPath, $file); $path = '/bs/image/' . $name; if (!empty($filename) && file_exists(app()->getRootPath() . 'public' . $path)) { $newName = app()->getRootPath() . 'public/bs/' . $filename; copy(app()->getRootPath() . 'public' . $path, $newName); unlink(app()->getRootPath() . 'public' . $path); $path = '/bs/' . $filename; } // 返回路径 return json(['code' => '0', 'message' => '上传成功', 'data' => ['path' => $path]]); } catch (\Exception $exception) { return json(['code' => '100500', 'message' => '系统繁忙', 'data' => [$exception->getMessage()]]); } } // 上传视频到aws s3 public function uploadVideo() { try { // 获取文件 $file = $this->request->file('file'); // 检测文件是否存在 if (!$file) { return json(['code' => 400, 'message' => 'No file uploaded']); } // 验证文件类型和大小 $maxSize = 1024 * 1024 * 1024; //限制1G if ($file->getSize() > $maxSize) { return json(['code' => 400, 'message' => 'The file size cannot exceed 1 GB']); } // 生成唯一的文件名 $fileName = uniqid() . '.' . $file->getOriginalExtension(); // 初始化s3客户端 $s3Config = Config::get('common.aws_s3'); $s3Client = new S3Client([ 'version' => 'latest', 'region' => $s3Config['aws_region'], 'credentials' => [ 'key' => $s3Config['aws_key'], 'secret' => $s3Config['aws_secret'], ], ]); // 上传文件到S3 $result = $s3Client->putObject([ 'Bucket' => $s3Config['aws_bucket'], 'Key' => 'bourse-video-node/' . $fileName, // s3中的存储路径 'Body' => fopen($file->getRealPath(), 'r'), 'ContentType' => $file->getOriginalMime(), // 设置文件的MIME, 不然s3会以流式存储 // 'ACL' => 'public-read', // 设置文件为公开可读 ]); // 记录上传的文件 $resData = AwsS3Model::create([ 'name' => $fileName, 's3_url' => $result['ObjectURL'], 'size' => $file->getSize(), 'mime' => $file->getOriginalMime(), 'ext' => $file->getOriginalExtension(), ]);; // 返回上传成功的地址 return json(['code' => '0', 'message' => '上传成功', 'data' => [ 'id' => $resData->id, 'url' => $result['ObjectURL'], 'name' => $fileName, 'size' => $file->getSize(), 'mime' => $file->getOriginalMime(), 'ext'=> $file->getOriginalExtension(), ]]); } catch (\Exception $exception) { return json(['code' => '100500', 'message' => '系统繁忙', 'data' => [$exception->getMessage()]]); } } // 初始化分片上传 public function initiateUpload(){ try { $param = $this->request->param(); if (empty($param['file_type'])) { return json(['code' => '100500', 'message' => '缺少参数 file_type', 'data' => []]); } // 允许的视频类型 $allowed_types = [ 'video/mp4', 'video/quicktime', 'video/x-msvideo', 'video/x-ms-wmv', 'video/x-matroska', ]; if (!in_array($param['file_type'], $allowed_types)) { return json(['code' => '100500', 'message' => '上传的文件类型不在允许范围内', 'data' => []]); } // 生成唯一的文件名 $fileName = uniqid('video_', true); // 初始化s3客户端 $s3Config = Config::get('common.aws_s3'); $s3Client = new S3Client([ 'version' => 'latest', 'region' => $s3Config['aws_region'], 'credentials' => [ 'key' => $s3Config['aws_key'], 'secret' => $s3Config['aws_secret'], ], ]); // 初始化Multipart Upload $result = $s3Client->createMultipartUpload([ 'Bucket' => $s3Config['aws_bucket'], 'Key' => 'bourse-video-node/' . $fileName, ]); return json([ 'code' => 0, 'data' => [ 'uploadId' => $result['UploadId'], 'key' => $result['Key'] ] ]); } catch (\Exception $exception) { return json(['code' => '100500', 'message' => '初始化上传失败', 'data' => [$exception->getMessage()]]); } } // 上传分片 public function uploadPart(){ try { $param = $this->request->param(); if (empty($param['uploadId']) || empty($param['key']) || empty($param['partNumber'])) { return json(['code' => 400, 'message' => '缺少参数']); } $file = $this->request->file('file'); if (!$file) { return json(['code' => 400, 'message' => 'No file uploaded']); } // 初始化s3客户端 $s3Config = Config::get('common.aws_s3'); $s3Client = new S3Client([ 'version' => 'latest', 'region' => $s3Config['aws_region'], 'credentials' => [ 'key' => $s3Config['aws_key'], 'secret' => $s3Config['aws_secret'], ], ]); $result = $s3Client->uploadPart([ 'Bucket' => $s3Config['aws_bucket'], 'Key' => $param['key'], 'PartNumber' => $param['partNumber'], 'UploadId' => $param['uploadId'], 'Body' => fopen($file->getRealPath(), 'r'), ]); return json([ 'code' => 200, 'data' => [ 'ETag' => $result['ETag'], 'PartNumber' => $param['partNumber'] ] ]); } catch (\Exception $exception) { return json(['code' => '100500', 'message' => '上传失败', 'data' => [$exception->getMessage()]]); } } // 完成上传 public function completeUpload(){ try { $param = $this->request->param(); if (empty($param['uploadId']) || empty($param['key']) || empty($param['parts'])) { return json(['code' => 400, 'message' => '缺少参数']); } // 初始化s3客户端 $s3Config = Config::get('common.aws_s3'); $s3Client = new S3Client([ 'version' => 'latest', 'region' => $s3Config['aws_region'], 'credentials' => [ 'key' => $s3Config['aws_key'], 'secret' => $s3Config['aws_secret'], ], ]); $s3Client->completeMultipartUpload([ 'Bucket' => $s3Config['aws_bucket'], 'Key' => $param['key'], 'UploadId' => $param['uploadId'], 'MultipartUpload' => [ 'Parts' => $param['parts'] ], ]); return json(['code' => 200, 'message' => '上传成功']); } catch (\Exception $exception) { return json(['code' => '100500', 'message' => '完成上传失败', 'data' => [$exception->getMessage()]]); } } // 取消上传 public function abortUpload(){ try { $param = $this->request->param(); if (empty($param['uploadId']) || empty($param['key'])) { return json(['code' => 400, 'message' => '缺少参数']); } // 初始化s3客户端 $s3Config = Config::get('common.aws_s3'); $s3Client = new S3Client([ 'version' => 'latest', 'region' => $s3Config['aws_region'], 'credentials' => [ 'key' => $s3Config['aws_key'], 'secret' => $s3Config['aws_secret'], ], ]); $s3Client->abortMultipartUpload([ 'Bucket' => $s3Config['aws_bucket'], 'Key' => $param['key'], 'UploadId' => $param['uploadId'], ]); return json(['code' => 200, 'message' => '上传已取消']); } catch (\Exception $exception) { return json(['code' => '100500', 'message' => '取消上传失败', 'data' => [$exception->getMessage()]]); } } }