upvd
This commit is contained in:
parent
eaaf2581ab
commit
32bb24ffc9
@ -14,13 +14,16 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.imooc.utils.QcCloud;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Api(tags = "FileController 文件上传测试的接口")
|
||||
@RestController
|
||||
public class FileController {
|
||||
|
||||
@Autowired
|
||||
private MinIOConfig minIOConfig;
|
||||
private MinIOConfig minIOConfig;
|
||||
|
||||
@PostMapping("upload")
|
||||
public GraceJSONResult upload(MultipartFile file) throws Exception {
|
||||
@ -39,4 +42,11 @@ public class FileController {
|
||||
|
||||
return GraceJSONResult.ok(imgUrl);
|
||||
}
|
||||
|
||||
private QcCloud qcCloud;
|
||||
@PostMapping("uploadVd")
|
||||
public GraceJSONResult uploadVd(MultipartFile file) throws Exception {
|
||||
String vdFileId = qcCloud.uploadViaTempFile(file);
|
||||
return GraceJSONResult.ok(vdFileId);
|
||||
}
|
||||
}
|
||||
|
@ -102,6 +102,14 @@
|
||||
<version>3.1.270</version>
|
||||
</dependency>
|
||||
|
||||
<!--腾讯云点播-->
|
||||
<dependency>
|
||||
<groupId>com.qcloud</groupId>
|
||||
<artifactId>vod_api</artifactId>
|
||||
<version>2.1.5</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
|
66
book-common/src/main/java/com/imooc/utils/QcCloud.java
Normal file
66
book-common/src/main/java/com/imooc/utils/QcCloud.java
Normal file
@ -0,0 +1,66 @@
|
||||
package com.imooc.utils;
|
||||
import com.qcloud.vod.model.VodUploadResponse;
|
||||
import com.qcloud.vod.VodUploadClient;
|
||||
import com.qcloud.vod.model.VodUploadRequest;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
|
||||
@Component
|
||||
public class QcCloud {
|
||||
@Autowired
|
||||
private TencentCloudProperties tencentCloudProperties;
|
||||
public String getSecretId() {
|
||||
return tencentCloudProperties.getSecretId();
|
||||
}
|
||||
|
||||
public String getSecretKey() {
|
||||
return tencentCloudProperties.getSecretKey();
|
||||
}
|
||||
|
||||
public String getRegion() {
|
||||
return "ap-guangzhou";// 腾讯云 VOD 和 COS 使用相同区域
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String uploadViaTempFile(MultipartFile file) throws Exception {
|
||||
// 1. 获取文件扩展名
|
||||
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
|
||||
|
||||
// 2. 获取当前时间戳和 UUID 作为文件名
|
||||
String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
||||
String uniqueFilename = "vod-" + timestamp + "-" + UUID.randomUUID().toString().replace("-", "").substring(8) + "." + extension;
|
||||
|
||||
// 3. 创建临时文件并转存
|
||||
Path tempFile = Files.createTempFile("vod-", "-" + uniqueFilename); // 创建临时文件
|
||||
file.transferTo(tempFile.toFile()); // 将文件上传内容写入临时文件
|
||||
|
||||
System.out.println("文件临时路径: " + tempFile.toString());
|
||||
|
||||
try {
|
||||
VodUploadClient client = new VodUploadClient(getSecretId(), getSecretKey());
|
||||
VodUploadRequest request = new VodUploadRequest();
|
||||
request.setMediaFilePath(tempFile.toString()); // 指定本地文件路径
|
||||
request.setProcedure("视频处理"); // 智能审核+水印
|
||||
// 4. 上传文件
|
||||
VodUploadResponse response = client.upload(getRegion(), request);
|
||||
return response.getFileId(); // 返回腾讯云 VOD 的 id
|
||||
} finally {
|
||||
// 5. 删除临时文件
|
||||
Files.deleteIfExists(tempFile);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user