COS腾讯云工具类

点击量:23

基于腾讯云的对象存储编写的工具类,以便于更加快捷的使用对象存储

基于腾讯云的对象存储工具类

直接创建工具类对象并粘贴以下代码即可

package com.li.web.controller;

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.GetObjectRequest;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.net.URL;
import java.util.Date;


/**
* author:lizhihang
* time:2019/6/15-17:19
* version:1.0
* discription:腾讯云对象存储文件上传下载工具类
* 使用前请导入COS-API 依赖
*/
public class COS_Utils {

   private static String ACCESS_KEY ="XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
   private static String SECRET_KEY ="XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
   private static String REGION_NAME = "XXXXXXX";
   private static String BUCKET_NAME = "XXXXXXXXXXXXX"; //BUCKET_NAME为 bucketName+appId

   private static COSClient cosClient = null;
   static {
        //初始化客户端
        COSCredentials cred = new BasicCOSCredentials(ACCESS_KEY, SECRET_KEY);
        Region region = new Region(REGION_NAME);
        ClientConfig clientConfig = new ClientConfig(region);
        cosClient = new COSClient(cred, clientConfig);
  }


   /**
    * File文件上传 并获取URL
    * @param file
    * @return 拼接URL
    */
   public String fileUpload(File file) {
       String name = file.getName();
       String suffix = name.substring(name.lastIndexOf("."));

       String key = new Date().getTime()+suffix;
       PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, key, file);
       cosClient.putObject(putObjectRequest);

       Date expiration = new Date(new Date().getTime() + 5 * 60 * 10000);
       URL url = cosClient.generatePresignedUrl(BUCKET_NAME, key, expiration);
       return url.toString();
  }


   /**
    * MutipartFile格式文件上传 并获取URL
    * @param multipartFile
    * @return 拼接URL
    */
   public String fileUpload(MultipartFile multipartFile) {
       CommonsMultipartFile commonsmultipartfile = (CommonsMultipartFile) multipartFile;
       DiskFileItem diskFileItem = (DiskFileItem) commonsmultipartfile.getFileItem();
       File file = diskFileItem.getStoreLocation();
       return fileUpload(file);
  }


   /**
    * 通过key获取文件
    * @param key
    * @param path 文件存放路径
    * @param name 文件名
    */
   public void FileDownload(String key,String path,String name) {
           File downFile = new File(path+name);
           GetObjectRequest getObjectRequest = new GetObjectRequest(BUCKET_NAME,key);
           ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
  }


   /**
    * 指定KEY上传文件
    * @param file
    * @param key
    */
   public void FileUpload(String key,File file){
       File localFile = file;
       PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, key, localFile);
       PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
  }


   /**
    * 根据key删除文件
    * @param key
    */
   public void deleteFileByKey(String key) {
       cosClient.deleteObject(BUCKET_NAME, key);
  }


   /**
    * 关闭COS客户端
    */
   public void shutdownClient() {
       cosClient.shutdown();
  }
}
注意
  • 使用前必须要导入COS-API的Maven依赖 或者相关jar包

  • 客户端使用完毕一定要注意关闭客户端

感谢您的阅读
文章由作者个人总结,欢迎指出文章错误



上一篇:SSM使用配置以及整合

下一篇:Kotlin(1)-基本语法