{"id":874,"date":"2023-03-09T21:46:58","date_gmt":"2023-03-09T13:46:58","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=874"},"modified":"2023-04-29T16:42:53","modified_gmt":"2023-04-29T08:42:53","slug":"spring-boot-upload-files-to-tencent-cloud-storage","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/09\/spring-boot-upload-files-to-tencent-cloud-storage\/","title":{"rendered":"Spring Boot\u4e0a\u4f20\u6587\u4ef6\u5230\u817e\u8baf\u4e91\u50a8\u5b58"},"content":{"rendered":"<h2>\u817e\u8baf\u4e91\u5e73\u53f0\u64cd\u4f5c<\/h2>\n<p>\u767b\u5f55\u5230\u817e\u8baf\u4e91\u63a7\u5236\u53f0 -&gt; \u5bf9\u8c61\u5b58\u50a8 -&gt; \u521b\u5efa\u5b58\u50a8\u6876\uff08\u8bbf\u95ee\u6743\u9650\u8bbe\u7f6e\u4e3a\u516c\u6709\u8bfb\u79c1\u6709\u5199\uff09<\/p>\n<p><!-- more --><\/p>\n<pre><code>\u7a7a\u95f4\u540d\u79f0    appblog-1253563213\n\u6240\u5c5e\u5730\u57df    \u5e7f\u5dde\uff08\u534e\u5357\uff09 \uff08ap-guangzhou\uff09\n\u8bbf\u95ee\u57df\u540d    https:\/\/appblog-1253563213.cos.ap-guangzhou.myqcloud.com<\/code><\/pre>\n<p>\u79d8\u94a5\u7ba1\u7406 -&gt; \u65b0\u5efa\u4e91API\u79d8\u94a5<\/p>\n<pre><code>SecretId: AKIDhBQQAjuGA6MehUITeZDbjcGessc9st88\nSecretKey: L2JK6JHEySV3LgmZSWehXAYsxoQn75kA<\/code><\/pre>\n<h2>Spring boot\u9879\u76ee\u96c6\u6210<\/h2>\n<h3>Maven\u4f9d\u8d56<\/h3>\n<pre><code class=\"language-xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/com.qcloud\/cos_api --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.qcloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;cos_api&lt;\/artifactId&gt;\n    &lt;version&gt;5.5.1&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>\u914d\u7f6e\u4fe1\u606f<\/h3>\n<pre><code class=\"language-yml\">tencent:\n  #\u817e\u8baf\u4e91\u5b58\u50a8\u6240\u9700\u4fe1\u606f\n  cloud:\n    #SecretId\n    accessKey: AKIDhBQQAjuGA6MehUITeZDbjcGessc9st88\n    #SecretKey\n    secretKey: L2JK6JHEySV3LgmZSWehXAYsxoQn75kA\n    #\u6240\u5c5e\u533a\u57df\n    bucket: ap-guangzhou\n    #\u5b58\u50a8\u6876\u540d\u79f0\n    bucketName: appblog-1253563213\n    #\u8bbf\u95ee\u57df\u540d\n    path: https:\/\/appblog-1253563213.cos.ap-guangzhou.myqcloud.com\n    #\u8def\u5f84\u524d\u7f00\n    prefix: appblog<\/code><\/pre>\n<pre><code class=\"language-java\">@Getter\n@Setter\n@Configuration\npublic class TencentConfig {\n    @Value(&quot;${tencent.cloud.accessKey}&quot;)\n    private String accessKey;\n    @Value(&quot;${tencent.cloud.secretKey}&quot;)\n    private String secretKey;\n    @Value(&quot;${tencent.cloud.bucket}&quot;)\n    private String bucket;\n    @Value(&quot;${tencent.cloud.bucketName}&quot;)\n    private String bucketName;\n    @Value(&quot;${tencent.cloud.path}&quot;)\n    private String path;\n    @Value(&quot;${tencent.cloud.prefix}&quot;)\n    private String prefix;\n}<\/code><\/pre>\n<h3>\u6587\u4ef6\u4e0a\u4f20\u63a7\u5236\u5668<\/h3>\n<pre><code class=\"language-java\">@Controller\n@Slf4j\n@RequestMapping(value = &quot;\/upload\/tencent&quot;)\npublic class TencentUploadController {\n    @Autowired\n    private TencentConfig tencentConfig;\n\n    \/**\n     * \u4e0a\u4f20\u9053\u817e\u8baf\u4e91\u670d\u52a1\u5668\uff08https:\/\/cloud.tencent.com\/document\/product\/436\/10199\uff09\n     * @return\n     *\/\n    @RequestMapping(value = &quot;\/single&quot;, method = RequestMethod.POST)\n    @ResponseBody\n    public Object Upload(@RequestParam(value = &quot;file&quot;) MultipartFile file, HttpSession session) {\n        if (file == null) {\n            return new UploadMsg(0, &quot;\u6587\u4ef6\u4e3a\u7a7a&quot;, null);\n        }\n        String oldFileName = file.getOriginalFilename();\n        String eName = oldFileName.substring(oldFileName.lastIndexOf(&quot;.&quot;));\n        String newFileName = UUID.randomUUID() + eName;\n        Calendar cal = Calendar.getInstance();\n        int year = cal.get(Calendar.YEAR);\n        int month = cal.get(Calendar.MONTH) + 1;\n        int day = cal.get(Calendar.DATE);\n        \/\/ 1 \u521d\u59cb\u5316\u7528\u6237\u8eab\u4efd\u4fe1\u606f(secretId, secretKey)\n        COSCredentials cred = new BasicCOSCredentials(tencentConfig.getAccessKey(), tencentConfig.getSecretKey());\n        \/\/ 2 \u8bbe\u7f6ebucket\u7684\u533a\u57df, COS\u5730\u57df\u7684\u7b80\u79f0\u8bf7\u53c2\u7167 https:\/\/cloud.tencent.com\/document\/product\/436\/6224\n        ClientConfig clientConfig = new ClientConfig(new Region(tencentConfig.getBucket()));\n        \/\/ 3 \u751f\u6210cos\u5ba2\u6237\u7aef\n        COSClient cosclient = new COSClient(cred, clientConfig);\n        \/\/ bucket\u7684\u547d\u540d\u89c4\u5219\u4e3a{name}-{appid} \uff0c\u6b64\u5904\u586b\u5199\u7684\u5b58\u50a8\u6876\u540d\u79f0\u5fc5\u987b\u4e3a\u6b64\u683c\u5f0f\n        String bucketName = tencentConfig.getBucketName();\n\n        \/\/ \u7b80\u5355\u6587\u4ef6\u4e0a\u4f20, \u6700\u5927\u652f\u6301 5 GB, \u9002\u7528\u4e8e\u5c0f\u6587\u4ef6\u4e0a\u4f20, \u5efa\u8bae 20 M \u4ee5\u4e0b\u7684\u6587\u4ef6\u4f7f\u7528\u8be5\u63a5\u53e3\n        \/\/ \u5927\u6587\u4ef6\u4e0a\u4f20\u8bf7\u53c2\u7167 API \u6587\u6863\u9ad8\u7ea7 API \u4e0a\u4f20\n        File localFile = null;\n        try {\n            localFile = File.createTempFile(&quot;temp&quot;, null);\n            file.transferTo(localFile);\n            \/\/ \u6307\u5b9a\u8981\u4e0a\u4f20\u5230 COS \u4e0a\u7684\u8def\u5f84\n            String key = &quot;\/&quot; + tencentConfig.getPrefix() + &quot;\/&quot; + year + &quot;\/&quot; + month + &quot;\/&quot; + day + &quot;\/&quot; + newFileName;\n            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);\n            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);\n            log.info(putObjectResult.getContentMd5());\n            log.info(putObjectResult.getDateStr());\n            log.info(putObjectResult.getETag());\n            log.info(putObjectResult.getRequestId());\n            \/\/{&quot;status&quot;:1,&quot;msg&quot;:&quot;\u4e0a\u4f20\u6210\u529f&quot;,&quot;path&quot;:&quot;https:\/\/appblog-1253563213.cos.ap-guangzhou.myqcloud.com\/appblog\/2019\/2\/20\/3f934239-2c92-4da1-bb80-896299fcdcdf.png&quot;}\n            return new UploadMsg(1, &quot;\u4e0a\u4f20\u6210\u529f&quot;, tencentConfig.getPath() + putObjectRequest.getKey());\n        } catch (IOException e) {\n            return new UploadMsg(-1, e.getMessage(), null);\n        } finally {\n            \/\/ \u5173\u95ed\u5ba2\u6237\u7aef(\u5173\u95ed\u540e\u53f0\u7ebf\u7a0b)\n            cosclient.shutdown();\n        }\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">@Getter\n@Setter\n@NoArgsConstructor\n@AllArgsConstructor\npublic class UploadMsg {\n    public int status;\n    public String msg;\n    public String path;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u817e\u8baf\u4e91\u5e73\u53f0\u64cd\u4f5c \u767b\u5f55\u5230\u817e\u8baf\u4e91\u63a7\u5236\u53f0 -&gt; \u5bf9\u8c61\u5b58\u50a8 -&gt; \u521b\u5efa\u5b58\u50a8\u6876\uff08\u8bbf\u95ee\u6743\u9650\u8bbe\u7f6e\u4e3a\u516c\u6709\u8bfb\u79c1\u6709\u5199\uff09 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[246],"class_list":["post-874","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-oss"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=874"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/874\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}