腾讯云的使用

腾讯云COS使用:

导入依赖:

 <!--        COS-->
        <dependency>
            <groupId>com.qcloud</groupId>
            <artifactId>cos_api-bundle</artifactId>
            <version>5.6.60</version>
        </dependency>

properties配置:


COS.secretId=

COS.secretKey=

COS.appId=
# 存储桶名字
COS.bucketName=
# 地区
COS.region=

COS.url=
 # 前缀
COS.prefix=

Config配置类:


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.region.Region;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Data
@Configuration
public class COSConfig {
    @Value("${COS.secretId}")
    private  String secretId;
    @Value("${COS.secretKey}")
    private String secretKey;
    @Value("${COS.appId}")
    private String appId;
    @Value("${COS.bucketName}")
    private String bucketName;
    @Value("${COS.region}")
    private String region;
    @Value("${COS.url}")
    private String url;
    @Value("${COS.prefix}")
    private String prefix;


    @Bean
    public COSClient cosClient() {
//        初始化用户信息
        COSCredentials cosCredentials = new BasicCOSCredentials(this.secretId,this.secretKey);
//        设置地域
        Region region = new Region(this.region);
        ClientConfig config = new ClientConfig(region);
//        生成COS客户端
        COSClient client = new COSClient(cosCredentials,config);
        return client;

    }
}

封装的工具类:



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.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.twogroup.shoppingwall.config.COSConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author Z
 */
@Slf4j
public class UploadUtil {

    @Resource
    private COSConfig cosConfig;

    @Resource
    private COSClient cosClient;

    public UploadUtil(COSConfig cosConfig,COSClient cosClient) {
        this.cosConfig = cosConfig;
        this.cosClient = cosClient;
    }

    private static ClientConfig clientConfig = new ClientConfig(new Region("ap-nanjing"));
    private static String bucketName = "";
    private static String s = "";

    public UploadUtil() {

    }


    /**
     * 上传文件
     * @param file
     * @return
     */
    public String uploadUtil(MultipartFile file) {
        try {
            InputStream is = new BufferedInputStream(file.getInputStream());
            String pic = UUID.randomUUID().toString().replace("-", "");
            //获取文件的原始名字
            String picName = file.getOriginalFilename();
            //截取后缀
            assert picName != null;
            String picType = cosConfig.getPrefix() + pic + picName.substring(picName.lastIndexOf("."));
            log.info("最后一部分"+picName.substring(picName.lastIndexOf(".")));
            ObjectMetadata objectMetadata = new ObjectMetadata();
            //设置流的大小
            objectMetadata.setContentLength(file.getSize());
            PutObjectRequest putObjectRequest = new PutObjectRequest(cosConfig.getBucketName(), picType, is, objectMetadata);
                PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
                if (putObjectResult.getETag() != null) {
                    //标识已经上传成功
                    System.out.println("上传成功:" + picType);
//                前缀:https://shopwall-1305869108.cos.ap-nanjing.myqcloud.com/
                    
                    return  picType;
                }

        } catch (IOException e) {
            //出现文件异常
            return null;
        }
        return null;
    }


    /**
     * 删除文件
     * @param path
     */
    public static boolean delete(String path) {
        COSConfig cosConfig = new COSConfig();
        COSCredentials cred = new BasicCOSCredentials("COS.secretId","COS.secretKey");
        String str = path.substring(0, s.length());
        if (!s.equals(str)) {
            return false;
        }
        final boolean[] flag = {false};
        new Thread(new Runnable() {
            @Override
            public void run() {
                COSClient cosclient = new COSClient(cred, clientConfig);
                String substring = path.substring(s.length()-1, path.length());
                // 指定要删除的 bucket 和路径.
                try {
                    cosclient.deleteObject(bucketName ,substring);
                    flag[0] = true;
                    System.out.println("删除成功");
                } catch (Throwable tb) {
                    System.out.println("删除文件失败");
                    tb.printStackTrace();
                }
            }
        }).start();
        return flag[0];
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值