UploadTencentCOSController.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //package com.backendsys.controller.Upload;
  2. //
  3. //import com.backendsys.entity.System.SysFile.SysFileDTO;
  4. //import com.backendsys.entity.Tencent.TencentCos.MultipartUploadDTO;
  5. //import com.backendsys.service.SDKService.SDKTencent.SDKTencentCOSService;
  6. //import com.backendsys.utils.CommonUtil;
  7. //import com.backendsys.utils.response.Result;
  8. //import com.backendsys.utils.response.ResultEnum;
  9. //import com.qcloud.cos.COSClient;
  10. //import com.qcloud.cos.ClientConfig;
  11. //import com.qcloud.cos.auth.BasicCOSCredentials;
  12. //import com.qcloud.cos.auth.COSCredentials;
  13. //import com.qcloud.cos.model.*;
  14. //import com.qcloud.cos.region.Region;
  15. //
  16. //import com.qcloud.cos.utils.IOUtils;
  17. //import jakarta.validation.constraints.NotEmpty;
  18. //import org.springframework.beans.factory.annotation.Autowired;
  19. //import org.springframework.beans.factory.annotation.Value;
  20. //import org.springframework.security.access.prepost.PreAuthorize;
  21. //import org.springframework.validation.annotation.Validated;
  22. //import org.springframework.web.bind.annotation.*;
  23. //import org.springframework.web.multipart.MultipartFile;
  24. //
  25. //import java.io.File;
  26. //import java.io.FileOutputStream;
  27. //import java.io.IOException;
  28. //import java.io.InputStream;
  29. //import java.text.SimpleDateFormat;
  30. //import java.util.Date;
  31. //import java.util.LinkedHashMap;
  32. //import java.util.Map;
  33. //
  34. //@Validated
  35. //@RestController
  36. //public class UploadTencentCOSController {
  37. //
  38. // @Value("${tencent.cos.max-size}")
  39. // private long maxSize;
  40. // @Value("${tencent.cos.secret-id}")
  41. // private String secretId;
  42. // @Value("${tencent.cos.secret-key}")
  43. // private String secretKey;
  44. // @Value("${tencent.cos.region}")
  45. // private String region;
  46. // @Value("${tencent.cos.bucket-name}")
  47. // private String bucketName;
  48. // @Value("${tencent.cos.accessible-domain}")
  49. // private String accessibleDomain;
  50. //
  51. // @Autowired
  52. // private SDKTencentCOSService sdkTencentCOSService;
  53. //
  54. //
  55. //// /**
  56. //// * 权限:待定
  57. //// * 获得临时上传密钥 (用于前端单文件上传、分片上传)
  58. //// */
  59. //// @GetMapping("/api/upload/getTempCredentials")
  60. //// public Result getTempCredentials() {
  61. //// return Result.success(sdkTencentCOSService.getTempCredentials("*"));
  62. //// }
  63. //
  64. //
  65. //// /**
  66. //// * 权限:待定
  67. //// * 获得素材文件列表
  68. //// */
  69. //// @GetMapping("/api/upload/getBucketList")
  70. //// public Result getBucketList(String next_marker) {
  71. //// return Result.success(sdkTencentCOSService.getBucketList(next_marker));
  72. //// }
  73. //
  74. //
  75. // // 【腾讯云 COS】
  76. // // 文档中心 > 对象存储 > API 文档 > Object 接口 > 基本操作 > PUT Object
  77. // // https://cloud.tencent.com/document/product/436/7749
  78. // // https://github.com/tencentyun/cos-java-sdk-v5/blob/master/src/main/java/com/qcloud/cos/demo/PutObjectDemo.java
  79. //
  80. // /**
  81. // * 上传文件 (旧)
  82. // */
  83. // @PreAuthorize("@sr.hasPermission(1.1)")
  84. // @PostMapping("/api/tencent/cosUpload")
  85. // public Result uploadTencentCOS(@RequestParam("file") MultipartFile file) {
  86. //
  87. // // 检查上传的文件是否为空
  88. // if (file.isEmpty()) {
  89. // return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "文件不能为空");
  90. // }
  91. // // 判断文件大小是否超过
  92. // if (file.getSize() > maxSize) {
  93. // return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "文件不能大于 " + maxSize/1024/1024 + " MB");
  94. // }
  95. //
  96. // // 按日期作为文件目录 (腾讯云使用 /,所以不需要配置 File.separator)
  97. // String dateDir = new SimpleDateFormat("yyyyMMdd").format(new Date()) + '/';
  98. //
  99. // // 生成后缀名
  100. // String originalFileName = file.getOriginalFilename();
  101. // String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
  102. //
  103. // // 生成新的文件名
  104. // String newFileName = CommonUtil.generateFilename(suffix);
  105. //
  106. // //
  107. // String objectName = dateDir + newFileName;
  108. //
  109. // // -- COS ------------------------------------------------------------------
  110. // // 初始化用户身份信息(secretId, secretKey)
  111. // COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
  112. //
  113. // // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
  114. // Region region1 = new Region(region);
  115. // ClientConfig clientConfig = new ClientConfig(region1);
  116. // clientConfig.setRegion(region1);
  117. //
  118. // // 生成cos客户端
  119. // COSClient cosClient = new COSClient(cred, clientConfig);
  120. //
  121. // try {
  122. //
  123. // // 获取文件内容
  124. // InputStream inputStream = file.getInputStream();
  125. //
  126. // // 将文件内容写入临时文件
  127. // File tempFile = File.createTempFile("temp", null);
  128. // try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
  129. // IOUtils.copy(inputStream, outputStream);
  130. // }
  131. //
  132. // // 创建 PutObjectRequest 对象。
  133. // PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, tempFile);
  134. //
  135. // ObjectMetadata objectMetadata = new ObjectMetadata();
  136. // putObjectRequest.withMetadata(objectMetadata);
  137. //
  138. // // 创建 PutObject 请求。(异步)
  139. // PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
  140. //
  141. // } catch (IOException e) {
  142. // System.out.println("IOException e:");
  143. // System.out.println(e);
  144. // } finally {
  145. // if (cosClient != null) {
  146. // cosClient.shutdown();
  147. // }
  148. // }
  149. //
  150. // // 返回值 结构
  151. // Map<String, Object> map = new LinkedHashMap<>();
  152. // map.put("filename", newFileName);
  153. // map.put("url", accessibleDomain + "/" + objectName); // File.separator
  154. // map.put("type", suffix.replace(".", ""));
  155. // map.put("size", file.getSize());
  156. //
  157. // return Result.success(map);
  158. //
  159. // }
  160. //
  161. //
  162. // /**
  163. // * 简单上传文件
  164. // */
  165. // @PreAuthorize("@sr.hasPermission(1.1)")
  166. // @PostMapping("/api/upload/simpleUpload")
  167. // public Result simpleUpload(@RequestParam("file") MultipartFile file) {
  168. // return Result.success(sdkTencentCOSService.simpleUpload(file));
  169. // }
  170. //
  171. //
  172. // /**
  173. // * 大文件分块上传
  174. // */
  175. // @PreAuthorize("@sr.hasPermission(1.1)")
  176. // @PostMapping("/api/upload/multipartUpload")
  177. // public Result multipartUpload(@Validated(MultipartUploadDTO.Upload.class) MultipartUploadDTO multipartUploadDTO) {
  178. // return Result.success(sdkTencentCOSService.multipartUpload(multipartUploadDTO));
  179. // }
  180. //
  181. // /**
  182. // * 查询已上传分块
  183. // */
  184. // @PreAuthorize("@sr.hasPermission(1.1)")
  185. // @GetMapping("/api/upload/listParts")
  186. // public Result listParts(@NotEmpty(message="upload_id 不能为空") String upload_id, @NotEmpty(message="key 不能为空") String key) {
  187. // return Result.success(sdkTencentCOSService.listParts(upload_id, key));
  188. // }
  189. //
  190. // /**
  191. // * 查询分块上传任务ID
  192. // */
  193. // @PreAuthorize("@sr.hasPermission(1.1)")
  194. // @GetMapping("/api/upload/getMultipartUploadIds")
  195. // public Result getMultipartUploadIds(@NotEmpty(message="key 不能为空") String key) {
  196. // return Result.success(sdkTencentCOSService.listMultipartUploads(key));
  197. // }
  198. //
  199. // /**
  200. // * 终止分块上传任务
  201. // */
  202. // @PreAuthorize("@sr.hasPermission(1.1)")
  203. // @PostMapping("/api/upload/abortMultipartUpload")
  204. // public Result abortMultipartUpload(@Validated(MultipartUploadDTO.Abort.class) @RequestBody MultipartUploadDTO multipartUploadDTO) {
  205. // return Result.success(sdkTencentCOSService.abortMultipartUpload(multipartUploadDTO));
  206. // }
  207. //
  208. // /**
  209. // * 删除文件
  210. // */
  211. // @PreAuthorize("@sr.hasPermission(1.1)")
  212. // @DeleteMapping("/api/upload/removeUploadFile")
  213. // public Result removeUploadFile(@Validated(SysFileDTO.Remove.class) @RequestBody SysFileDTO sysFileDTO) {
  214. // return Result.success(sdkTencentCOSService.removeUploadFile(sysFileDTO));
  215. // }
  216. //
  217. //}