• 首页
  • 邻居
  • 关于
  • 归档
  • 搜索
  • 夜间模式
    ©2020-2026  我的学习笔记 Theme by OneBlog

    我的学习笔记博客

    搜索
    标签
    # 随笔 # Java # 教程 # openwrt # Mysql # SQL # 爬虫 # post # Js调优 # MAVEN
  • 首页>
  • 随笔>
  • 正文
  • springboot通过文件流的方式上传文件到腾讯云cos代码记录

    2022年04月23日 1.8 k 阅读 0 评论 6206 字

    前言

    以下采用文件流的方式上传文件到腾讯云cos

    COSConfig

    @Data
    @Component("cosConfig")
    @ConfigurationProperties(prefix = "cos")
    public class COSConfig {
      private String baseUrl;
      private String secretId;
      private String secretKey;
      private String regionName;
      private String bucketName;
      private String folderPrefix;
    }

    COSClientUtil

    @Slf4j
    public class COSClientUtil {
    
        /**
         * 获取配置信息
         */
        private static COSConfig cosConfig = SpringUtilsAuTo.getBean(COSConfig.class);
    
        /**
         * 初始化用户身份信息
         */
        private static COSCredentials cred =
                new BasicCOSCredentials(cosConfig.getSecretId(), cosConfig.getSecretKey());
    
        /**
         * 设置bucket的区域
         */
        private static ClientConfig clientConfig =
                new ClientConfig(new Region(cosConfig.getRegionName()));
    
        /**
         * 生成COS客户端
         */
        private static COSClient cosClient = new COSClient(cred, clientConfig);
    
        /**
         * 上传文件
         *
         * @param file
         * @return
         * @throws Exception
         */
        public static String upload(MultipartFile file) throws Exception {
            String date = DateUtils.formatDate(new Date(), "yyyy-MM-dd");
            String originalFilename = file.getOriginalFilename();
            String nextId = IdGen.nextId();
            String name = nextId + originalFilename.substring(originalFilename.lastIndexOf("."));
            String folderName = cosConfig.getFolderPrefix() + "/" + date + "/";
            String key = folderName + name;
            File localFile = null;
            try {
                localFile = transferToFile(file);
                String filePath = uploadFileToCOS(localFile, key);
                log.info("upload COS successful: {}", filePath);
                return filePath;
            } catch (Exception e) {
                e.printStackTrace();
                throw new Exception("文件上传失败");
            } finally {
                localFile.delete();
            }
        }
    
        /**
         * 上传文件
         *
         * @param input
         * @param metadata
         * @param fileType
         * @return
         * @throws Exception
         */
        public static String inputStreamUpload(
                InputStream input, ObjectMetadata metadata, String fileType) throws InterruptedException {
            String date = DateUtils.formatDate(new Date(), "yyyy-MM-dd");
            String nextId = IdGen.nextId();
            String name = nextId + "." + fileType;
            String folderName = cosConfig.getFolderPrefix() + "/" + date + "/";
            String key = folderName + name;
            String filePath = inputStreamUploadFileToCOS(key, input, metadata);
            log.info("upload COS successful: {}", filePath);
            return filePath;
        }
    
        /**
         * 上传文件到COS
         *
         * @param localFile
         * @param key
         * @return
         */
        private static String uploadFileToCOS(File localFile, String key) throws InterruptedException {
            PutObjectRequest putObjectRequest =
                    new PutObjectRequest(cosConfig.getBucketName(), key, localFile);
            ExecutorService threadPool = Executors.newFixedThreadPool(8);
            // 传入一个threadPool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池
            TransferManager transferManager = new TransferManager(cosClient, threadPool);
            // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常
            Upload upload = transferManager.upload(putObjectRequest);
            UploadResult uploadResult = upload.waitForUploadResult();
            // transferManager.shutdownNow();
            // cosClient.shutdown();
            String filePath = cosConfig.getBaseUrl() + uploadResult.getKey();
            return filePath;
        }
    
        /**
         * 通过流的方式上传文件到COS
         *
         * @param key
         * @param input
         * @param metadata
         * @return
         * @throws InterruptedException
         */
        private static String inputStreamUploadFileToCOS(
                String key, InputStream input, ObjectMetadata metadata) throws InterruptedException {
            PutObjectRequest putObjectRequest =
                    new PutObjectRequest(cosConfig.getBucketName(), key, input, metadata);
            ExecutorService threadPool = Executors.newFixedThreadPool(8);
            // 传入一个threadPool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池
            TransferManager transferManager = new TransferManager(cosClient, threadPool);
            // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常
            Upload upload = transferManager.upload(putObjectRequest);
            UploadResult uploadResult = upload.waitForUploadResult();
            //  transferManager.shutdownNow();
            // cosClient.shutdown();
            String filePath = cosConfig.getBaseUrl() + uploadResult.getKey();
            return filePath;
        }
    
        public static void delete(String key) {
            // 指定要删除的 bucket 和对象键
            cosClient.deleteObject(cosConfig.getBucketName(), key.replaceAll(cosConfig.getBaseUrl(), ""));
        }
    
        /**
         * 用缓冲区来实现这个转换, 即创建临时文件 使用 MultipartFile.transferTo()
         *
         * @param multipartFile
         * @return
         */
        private static File transferToFile(MultipartFile multipartFile) throws IOException {
            String originalFilename = multipartFile.getOriginalFilename();
            String prefix = originalFilename.split("\\.")[0];
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            File file = File.createTempFile(prefix, suffix);
            multipartFile.transferTo(file);
            return file;
        }

    使用方法

     // 创建上传Object的Metadata
                    ObjectMetadata meta = new ObjectMetadata();
                    // 必须设置ContentLength
                    meta.setContentLength(Long.valueOf(objectMap.get("contentLength").toString()));
                    String fileType = objectMap.get("contentType").toString();
                    String filePath = COSClientUtil.inputStreamUpload(inputStreamFromUrl, meta, fileType);
              

    application.yml

    cos:
      baseUrl: https://****.myqcloud.com
      secretId: AKI****vKS****eqlOkpdBS
      secretKey: 0jBMHv****QQ8ZmjPo
      regionName: ap-****
      bucketName: ****-1253487179
      folderPrefix: /files
    本文著作权归作者 [ admin ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    取消回复

    发表留言
    回复

    首页邻居关于归档
    Copyright©2020-2026  All Rights Reserved.  Load:0.017 s
    京ICP备18019712号
    Theme by OneBlog V3.6.5
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。