• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用腾讯云COS对象存储详细流程

武飞扬头像
零
帮助1

一、开通COS对象存储

学新通

 二、创建存储桶

学新通

 基本信息

学新通

  • 所属地域:请选择与您业务(或用户数量)相对集中的物理区域所对应的 COS 地域,设置后不可修改。地域的更多信息请参见 地域和访问域名
  • 名称:请输入自定义的存储桶名称。设置后不可修改。命名说明请参见存储桶的 命名规范
  • 访问权限:存储桶默认提供三种访问权限:私有读写、公有读私有写和公有读写,设置后仍可修改。详细信息请参见 存储桶访问权限
  • 请求域名:自动生成。创建完存储桶后,您可以使用该域名对存储桶进行访问。

学新通

  • 版本控制:开启后,上传的同名对象将保存历史版本。
  • 日志存储:为您记录跟存储桶操作相关的各种请求日志。
  • 存储桶标签:存储桶标签作为管理存储桶的一个标识,您可以为存储桶设置标签,便于分组管理存储桶,详情请参见 设置存储桶标签
  • 服务端加密:目前存储桶的加密方式支持 SSE-COS 加密(即由 COS 托管密钥的服务端加密)。关于服务端加密的介绍,请参见 服务端加密概述

学新通

对存储桶的配置信息进行确认。如需修改,单击上一步即可。

学新通

 三、获取秘钥

 学新通

 1.创建子账号

学新通

学新通 四、将COS整合到spring Boot项目中

1.导入依赖

  1.  
    <dependency>
  2.  
    <groupId>com.qcloud</groupId>
  3.  
    <artifactId>cos_api</artifactId>
  4.  
    <version>5.6.89</version>
  5.  
    </dependency>

2.导入配置信息可放入yml文件中,使用工具类读取(相对安全)

学新通 

  1.  
    tencent:
  2.  
    cos:
  3.  
    #腾讯云对象存储参数
  4.  
    #腾讯云账户secretId,secretKey
  5.  
    secretId:
  6.  
    secretKey:
  7.  
    #存储桶名称
  8.  
    buckerName:
  9.  
    #地域
  10.  
    region: ap-shanghai
  11.  
    #请求域名(使用对应的存储桶和对应的地域)
  12.  
    url:

3.工具类

  1.  
     
  2.  
    @Component
  3.  
    public class ConstantCosUtils implements InitializingBean {
  4.  
     
  5.  
    @Value("${tencent.cos.secretId}")
  6.  
    public String secretId;
  7.  
     
  8.  
    @Value("${tencent.cos.secretKey}")
  9.  
    public String secretKey;
  10.  
     
  11.  
    @Value("${tencent.cos.buckerName}")
  12.  
    public String buckerName;
  13.  
     
  14.  
    @Value("${tencent.cos.region}")
  15.  
    public String region;
  16.  
     
  17.  
    @Value("${tencent.cos.url}")
  18.  
    public String url;
  19.  
     
  20.  
    public static String SECRET_ID;
  21.  
    public static String SECRET_KEY;
  22.  
    public static String BUCKET_NAME;
  23.  
    public static String REGION;
  24.  
    public static String URL;
  25.  
     
  26.  
     
  27.  
    @Override
  28.  
    public void afterPropertiesSet() throws Exception {
  29.  
    SECRET_ID = secretId;
  30.  
    SECRET_KEY = secretKey;
  31.  
    BUCKET_NAME = buckerName;
  32.  
    REGION = region;
  33.  
    URL = url;
  34.  
    }
  35.  
    }
学新通

 4.编写代码

controller

  1.  
    /**
  2.  
    * 上传商家logo
  3.  
    * @return
  4.  
    */
  5.  
    @ApiOperation("上传商家logo")
  6.  
    @PostMapping("/upload")
  7.  
    public R upload(MultipartFile file) {
  8.  
    String url = settingReceiptCustomService.upload(file);
  9.  
    return R.ok(url);
  10.  
    }

serviceImpl

  1.  
     
  2.  
    @Service
  3.  
    public class SettingReceiptCustomServiceImpl extends ServiceImpl<SettingReceiptCustomMapper, SettingReceiptCustom> implements SettingReceiptCustomService {
  4.  
     
  5.  
     
  6.  
    @Override
  7.  
    public String upload(MultipartFile file) {
  8.  
    COSClient cosClient = initCos();
  9.  
    try {
  10.  
    String filename = file.getOriginalFilename();
  11.  
    InputStream inputStream = file.getInputStream();
  12.  
    String filePath = getFilePath(filename);
  13.  
    // 上传文件
  14.  
    cosClient.putObject(new PutObjectRequest(ConstantCosUtils.BUCKET_NAME, filePath, inputStream, null));
  15.  
    cosClient.setBucketAcl(ConstantCosUtils.BUCKET_NAME, CannedAccessControlList.PublicRead);
  16.  
     
  17.  
    return ConstantCosUtils.URL "/" filePath;
  18.  
    } catch (Exception e) {
  19.  
    e.printStackTrace();
  20.  
    } finally {
  21.  
    cosClient.shutdown();
  22.  
    }
  23.  
    return null;
  24.  
    }
  25.  
     
  26.  
    private String getFilePath(String fileName){
  27.  
    String filePath = "logo/";
  28.  
    String fileType = fileName.substring(fileName.lastIndexOf("."));
  29.  
    filePath = IdUtils.randomUUID() fileType;
  30.  
    return filePath;
  31.  
    }
  32.  
     
  33.  
    /**
  34.  
    * 初始化COSClient
  35.  
    * @return
  36.  
    */
  37.  
    private COSClient initCos(){
  38.  
    // 1 初始化用户身份信息(secretId, secretKey)
  39.  
    BasicCOSCredentials credentials = new BasicCOSCredentials(ConstantCosUtils.SECRET_ID, ConstantCosUtils.SECRET_KEY);
  40.  
    // 2 设置 bucket 的区域, COS 地域的简称请参照
  41.  
    Region region = new Region(ConstantCosUtils.REGION);
  42.  
    ClientConfig clientConfig = new ClientConfig(region);
  43.  
    //5.6.54 版本开始,默认使用了 https
  44.  
    // clientConfig.setHttpProtocol(HttpProtocol.https);
  45.  
    // 3 生成 cos 客户端。
  46.  
    return new COSClient(credentials, clientConfig);
  47.  
    }
  48.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhgabi
系列文章
更多 icon
同类精品
更多 icon
继续加载