一、参考项

腾讯云 COS(官网): 对象存储数据处理_COS数据处理_数据处理方案-腾讯云

COS SDK for Java(官网): 对象存储 快速入门 - SDK 文档 - 文档中心 - 腾讯云

二、引入Pom文件

<!-- https://mvnrepository.com/artifact/com.qcloud/cos_api -->
<dependency><groupId>com.qcloud</groupId><artifactId>cos_api</artifactId><version>5.5.5</version><exclusions><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions>
</dependency>

三、定义抽象类

public abstract class BaseObjectStorage {/*** 上传文件** @param pathAndName* @param file*/public abstract void upload(String pathAndName, File file);/*** 授权** @param pathAndName* @param time* @return*/public abstract String authorize(String pathAndName, long time);/*** 授权(路径全)** @param pathAndName* @param time* @return*/public abstract String authorizeAllName(String pathAndName, long time);/*** 临时上传文件授权** @param dir* @return*/public abstract Map<String, Object> tokens(String dir);/*** 删除文件** @param pathAndName*/public abstract void deleteFile(String pathAndName);}

四、COS实现类

package cn.xhh.xhh.core.objectstorage;import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import com.google.common.collect.Maps;
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.auth.COSSigner;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.region.Region;
import lombok.Data;@Component
public class CosObjectStorage extends BaseObjectStorage {@Data@Component@ConfigurationProperties(prefix = "oss")public static class CosInfo {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;private String region;private String rootDirectory;}@Autowiredprivate CosInfo cosInfo;private COSClient init() {COSCredentials cred = new BasicCOSCredentials(cosInfo.accessKeyId, cosInfo.accessKeySecret);Region region = new Region(cosInfo.getRegion());ClientConfig clientConfig = new ClientConfig(region);COSClient cosClient = new COSClient(cred, clientConfig);return cosClient;}@Overridepublic void upload(String pathAndName, File file) {COSClient cosClient = init();try {cosClient.putObject(cosInfo.bucketName, cosInfo.rootDirectory + pathAndName, file);} finally {cosClient.shutdown();}}@Overridepublic String authorize(String pathAndName, long time) {COSClient cosClient = init();try {Date expiration = new Date(new Date().getTime() + time);URL url = cosClient.generatePresignedUrl(cosInfo.bucketName,cosInfo.rootDirectory + pathAndName, expiration);return url.toString();} finally {cosClient.shutdown();}}@Overridepublic String authorizeAllName(String pathAndName, long time) {COSClient cosClient = init();try {Date expiration = new Date(new Date().getTime() + time);URL url = cosClient.generatePresignedUrl(cosInfo.bucketName, pathAndName, expiration);return url.toString();} finally {cosClient.shutdown();}}@Overridepublic Map<String, Object> tokens(String dir) {Map<String, Object> result = Maps.newHashMap();COSClient cosClient = init();try {long expireTime = 60;long expireEndTime = System.currentTimeMillis() + expireTime * 1000;Date expiration = new Date(expireEndTime);COSCredentials cred = new BasicCOSCredentials(cosInfo.accessKeyId, cosInfo.accessKeySecret);COSSigner signer = new COSSigner();dir = "frontend/xhh/" + dir;String sign = signer.buildAuthorizationStr(HttpMethodName.PUT, dir, cred, expiration);result.put("storeType", "cos");result.put("storageType", "cos");result.put("accessId", cosInfo.accessKeyId);result.put("signature", sign);result.put("expire", String.valueOf(expireEndTime / 1000));result.put("dir", dir);result.put("host", cosInfo.endpoint.split("://")[0] + "://" + cosInfo.bucketName + "."+ cosInfo.endpoint.split("://")[1]);} catch (Exception e) {e.printStackTrace();} finally {cosClient.shutdown();}return result;}@Overridepublic void deleteFile(String pathAndName) {COSClient cosClient = init();try {cosClient.deleteObject(cosInfo.bucketName, cosInfo.rootDirectory + pathAndName);} finally {cosClient.shutdown();}}}

五、application配置文件

objectstorage.type: cos
cos:endpoint: https://oss-cn-xhh.aliyuncs.comregion: 1access-key-id: LTAI4GKpXXXXXYuEzsyY63xaccess-key-secret: vsVyXXX8XYWOWYjFMPXXXXjkwbucket-name: dataroot-directory: xhh/export/

SpringBoot整合腾讯云COS相关推荐

  1. springboot整合腾讯云cos对象储存

    一:腾讯云前期准备 直接在腾讯云中搜索"对象存储",立即使用 点击存储桶列表,创建存储桶 填写基本信息:所属地域,名称,访问权限(公有读写) 下一步,下一步,创建,存储桶创建成功 ...

  2. SpringBoot整合腾讯云COS对象存储实现文件上传

    企业级项目开发中都会有文件.图片.视频等文件上传并能够访问的场景,对于初学者Demo可能会直接存储在应用服务器上:对于传统项目可能会单独搭建FastDFS.MinIO等文件服务来实现存储,这种方案可能 ...

  3. SpringBoot整合腾讯云COS(上传)

    腾讯云COS文档:对象存储 快速入门-SDK 文档-文档中心-腾讯云 (tencent.com) 开通腾讯云COS 创建存储桶 请求域名可做拼接文件访问URL使用 然后下一步即可 上传文件时需要以上红 ...

  4. springboot整合腾讯云cos进行上传、下载、删除文件

    参考腾讯官方文档 pom <dependency><groupId>com.qcloud</groupId><artifactId>cos_api< ...

  5. Springboot整合腾讯云发短信服务图文并茂啰嗦

    Springboot调用腾讯云发短信服务 腾讯云官网 https://cloud.tencent.com 搜索短信,来到短信文档 https://cloud.tencent.com/document/ ...

  6. SpringBoot集成腾讯云COS存储

    河南循中网络科技有限公司 - 精心创作,详细分解,按照步骤,均可成功! 文章目录 学习资料 集成腾讯云COS存储 添加pom依赖 common的pom文件 yaml配置 创建TencentCosUti ...

  7. tp6整合腾讯云cos上传

    1.创建一个名为 composer.json的文件,内容如下: { "require": {"qcloud/cos-sdk-v5": ">=2. ...

  8. SpringBoot集成腾讯云存储COS服务

    前言 该文章会先简单的介绍一下腾讯云的COS存储,然后演示如何在SpringBoot项目中集成COS,每一步都有记录,保证初学者也能看懂. 文章目录 前言 1.腾讯云对象存储介绍 1.1.开通&quo ...

  9. Java springboot项目引入腾讯云COS实现上传

    Java springboot项目引入腾讯云COS实现上传 pom.xml 配置类CosConfig.java 上传工具类CosClientUtil.java pom.xml <!--腾讯云上传 ...

  10. 硅谷课堂 06_整合腾讯云对象存储和课程分类管理

    硅谷课堂第六天-整合腾讯云对象存储和课程分类管理 文章目录 硅谷课堂第六天-整合腾讯云对象存储和课程分类管理 一.讲师管理模块整合腾讯云对象存储 1.腾讯云对象存储介绍 1.1.开通"对象存 ...

最新文章

  1. 经典算法题每日演练——第六题 协同推荐SlopeOne 算法
  2. 烂泥:下载酷我收费的MV
  3. qt 怎么设计个性化的滑块_小小滑块大大学问,你真的会用滑块了吗?
  4. 上班族的10大经典哲学,还有什么能难倒你?[轉自太平洋電腦網]
  5. MyBatis-Plus_查询进阶01
  6. 两个vlan如何互通_网络交换机VLAN的常识与划分方法,你知道吗?
  7. Chrome格式化json
  8. hadoop yarn如何启动聚合日志
  9. 计算机硬盘除了c盘其他全不见了,电脑除了c盘其他盘都不见了
  10. 本科毕设研究记录(一)————小样本综述
  11. java中lastmodified_Java File lastModified()用法及代码示例
  12. 服务器vmware私有云,方案建议-使用VMware架构搭建自己的私有云.pptx
  13. 数据库插入数据时报错(使用sqlyog创建数据库表插入中文数据时报错Incorrect string value: ‘\xE4\xBB\x8E\xE5\x85\xA5…’ for column ‘)
  14. 如何更改HomePod使用的Apple ID?
  15. Spring Boot 之---什么是热部署?---怎么使用?
  16. 区块链共识算法及应用研究
  17. 100量子比特量子计算机,51量子比特模拟器问世,大规模量子计算机迈出重要一步...
  18. 浙江省2019年高考计算机排名,2019年高考各省文理科第一名去向,计算机大热
  19. 火狐怎么导入收藏夹_火狐浏览器怎么把网页添加到收藏夹
  20. 极致、简约、专业、优质的在线简历

热门文章

  1. Ant Design Pro (五) 修改Footer
  2. 微信管理工具用什么比较好呀
  3. 小米air耳机重新配对_小米air耳机重新配对_「小三爷出品」不错的新年礼物,小米蓝牙耳机Air体验...
  4. 华为模拟器ensp下载地址
  5. [Shader2D]浮雕效果
  6. VBA笔记 退出循环、Sub、Fuction等的Exit语句
  7. php实现微信公众号群发消息接口(thinkphp3.2.3)
  8. 使用adb安装apk
  9. 2020互联网公司中秋礼盒大比拼!
  10. 【OpenCV学习】cvtColor