1. 程式人生 > >springboot上傳檔案到騰訊云云儲存

springboot上傳檔案到騰訊云云儲存

一、環境準備

  • 騰訊云云儲存
  • 建立一個儲存桶

二、匯入maven工程依賴

<!-- 騰訊雲儲存 -->
<dependency>
    <groupId>com.qcloud</groupId>
    <artifactId>cos_api</artifactId>
    <version>5.2.4</version>
</dependency>
<dependency>
    <groupId>com.qcloud</groupId>
    <artifactId>qcloud-java-sdk</artifactId>
    <version>2.0.1</version>
</dependency>
<!--multipartfile轉換file-->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

三、配置檔案

qcloud:
  appID: 125725xxx
  secretId: AKID*********1y5PovTq
  secretKey: L6m*******4FG8pM
  bucketName: shw-****-1257256
  region: ap-beijing

四、書寫工具類

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;

@Component
public class QCloudUtil {

    @Value("${qcloud.appID}")
    private Long appID;
    
    @Value("${qcloud.secretId}")
    private String secretId;
    
    @Value("${qcloud.secretKey}")
    private String secretKey;
    
    @Value("${qcloud.bucketName}")
    private String bucketName;
    
    @Value("${qcloud.region}")
    private String region;

    /**
     * 上傳檔案到騰訊雲
     * @return
     */
    public String uploadFile(File file,String key){
        COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);
        // 設定bucket的區域, COS地域的簡稱請參照
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        //生成cos客戶端
        COSClient cosClient = new COSClient(cred, clientConfig);
        // 簡單檔案上傳, 最大支援 5 GB, 適用於小檔案上傳, 建議 20 M 以下的檔案使用該介面
        // 大檔案上傳請參照 API 文件高階 API 上傳
        // 指定要上傳到 COS 上的路徑
        
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        cosClient.putObject(putObjectRequest);
        cosClient.shutdown();
        String url="https://"+bucketName+".cos."+region+".myqcloud.com/"+key;
        return url;
    }
}

相關推薦

springboot檔案云云儲存

一、環境準備 騰訊云云儲存 建立一個儲存桶 二、匯入maven工程依賴 <!-- 騰訊雲儲存 --> <dependency> <groupId>com.qcloud</groupId> <

副文字編輯器 KindEditor 實現圖片雲物件儲存 COS

目錄   一、主要功能實現 二、效果圖 三、需要匯入的包 四、前端程式設計 五、後臺程式設計 六、github 下載 附加內容: 一、主要功能實現 1、配置 KindEditor  2、在 KindEditor 中實現圖片上傳

5行程式碼實現微信小程式圖片免費5G儲存空間的使用

本文介紹瞭如何在微信小程式開發中使用騰訊官方提供的雲開發功能快速實現圖片的上傳與儲存,以及介紹雲開發的 5G 儲存空間的基本使用方法,這將大大提高微信小程式的開發效率,同時也是微信小程式系列教程的視訊版更新的文字版本摘要。 此文為 「60 節實戰課微信小程式開發視訊教程」 的第 51 小節

eclipse 整合maven、git。 eclipse 建立maven專案並git(或阿里雲git、gitlab、github、公司內部git伺服器等git倉庫)

我們先起git倉庫和專案起好名字 git倉庫名:TestGit 專案名:TestGit   1、下載安裝git: https://blog.csdn.net/xiaoshizi4/article/details/83478594 2、下載安裝maven:&nb

springBoot檔案大小設定

引用參照:https://blog.csdn.net/xiaoni815/article/details/50525145 純粹為了記錄一下,具體分析見引用連結。 報錯上傳檔案大小受到限制,預設1M。 修改router服務和對應的應用服務的application.yml,只改應用服務依然

springboot檔案超限BUG

[org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateExcepti

關於springboot檔案報錯:The temporary upload location ***is not valid

  在執行springboot時 ,長時間執行後報錯 the temporary uplaod location *** is not valid  查過資料後發現是centos對‘/temp’下檔案自動清理的原因。  在springboot專案啟動後 系統會在‘/temp’目

springboot 檔案的程式碼

後端: @RequestMapping("/upload") public String upload(@RequestParam(“pic”)MultipartFile file, HttpServletRequest request){ String contentType = file

JS+HTML5實現前端的圖片壓縮的COS

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <meta name="viewport" content="w

SpringBoot 檔案到linux伺服器 異常java.io.FileNotFoundException: /tmp/tomcat.50898……解決方案

SpringBoot 上傳檔案到linux伺服器報錯java.io.FileNotFoundException: /tmp/tomcat.50898……報錯原因:解決方法 java.io.IOException: java.io.FileNotFoundExce

第 23 講 SpringBoot檔案

第二十三講 SpringBoot上傳檔案 1. 引入依賴:pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifact

SpringBoot檔案到本伺服器 目錄與jar包同級

前言 看標題好像很簡單的樣子,但是針對使用jar包釋出SpringBoot專案就不一樣了。 當你使用tomcat釋出專案的時候,上傳檔案存放會變得非常簡單,因為你可以隨意操作專案路徑下的資源。但是當你使用SpringBoot的jar包釋出專案的時候,你會發現,你不能像以前一樣操作檔案了。當你使用File

Springboot檔案

Controler層裡面程式碼內容 @RequestMapping(value="/uploadImg", method=RequestMethod.POST) @ResponseBody public String uploadImgController(@R

springboot檔案大小限制的配置

上傳檔案檔案過大時出現如下錯誤:org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field pic exceeds its maximum pe

springboot 檔案並回寫

做的時候查看了很多部落格,也遇到了很多不經意的問題,如下 1,關於上傳路徑的問題,由於springboot內建tomcat,打包之後為jar包,無法上傳檔案至專案內部,查詢網上有很多解決方案,未見生效,下面為我的解決方案:上傳至伺服器預設資料夾: 程式碼如下: 首先是設

springboot檔案的訪問

一、上傳 public @ResponseBody ApiResult<UploadResult> u

springboot 頭像 檔案儲存 檔案流返回瀏覽器檢視 區分作業系統 windows 7 or linux

//我的會員中心 頭像上傳介面/*windows 除錯*/@Value("${appImg.location}")private String winPathPic;/*linux 使用*/@Value("${img.location}")private String linuxPathPic;@PostM

Windows Server 2012 R2配置IIS及配置FTP檔案,超詳細雲伺服器配置步驟,阿里雲,

Windows Server 2012 R2配置IIS及配置FTP上傳檔案,超詳細雲伺服器配置步驟,阿里雲,騰訊雲 注意事項 配置並使用IIS 配置並使用FTP傳送檔案 注意事項 1、注意伺服器的防火牆是否關閉或者開

雲伺服器新增ftp使用者檔案

1. 安裝vsftpd sudo apt-get install vsftp 2. 建立ftp的工作目錄 mkdir /home/ftp 3. 新建ftp使用者並設定密碼以及工作目錄 sudo useradd -d /home/ftp -s /bin/bash ft

本地WinSCP檔案雲伺服器顯示Permission Denied

本地WinSCP上傳檔案到騰訊雲伺服器顯示 Permission Denied 從字面看,就是沒有許可權。這時我首先想到了騰訊雲伺服器的安全組。先去設定安全組。 1.設定安全組 我們用的是騰訊的雲伺服器,它預設有對伺服器的內外網訪問許可權控制,也就是說是這個訪問控制把我