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

前言

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

COSConfig

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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;
}</code></pre></div></div><p><code>使用方法</code></p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> // 创建上传Object的Metadata
            ObjectMetadata meta = new ObjectMetadata();
            // 必须设置ContentLength
            meta.setContentLength(Long.valueOf(objectMap.get(&#34;contentLength&#34;).toString()));
            String fileType = objectMap.get(&#34;contentType&#34;).toString();
            String filePath = COSClientUtil.inputStreamUpload(inputStreamFromUrl, meta, fileType);</code></pre></div></div><p><code>application.yml</code></p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">cos:

baseUrl: https://.myqcloud.com
secretId: AKI
vKSeqlOkpdBS
secretKey: 0jBMHv
QQ8ZmjPo
regionName: ap-****
bucketName: ****-1253487179
folderPrefix: /files