springboot通过文件流的方式上传文件到腾讯云cos代码记录

warning: 这篇文章距离上次修改已过356天,其中的内容可能已经有所变动。

前言

以下采用文件流的方式上传文件到腾讯云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
版权声明 ▶ 本网站名称:我的学习笔记
▶ 本文链接:https://ooolo.net/article/147.html
▶ 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行核实删除。
▶ 转载本站文章需要遵守:商业转载请联系站长,非商业转载请注明出处!!!

none
最后修改于:2023年04月28日 13:33

添加新评论

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png