天天看点

腾讯云--OOS对象存储服务--java程序封装

java对象存储工具类

package top.yuechenc.xingchen.common.utils;

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.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import com.tencent.cloud.CosStsClient;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

/**
 * @description: 腾讯对象存储服务
 * @author: Zhiwei Wang
 * @create: 2020/12/30
 */
@Data
@Component
@PropertySource("other.yml")
@ConfigurationProperties(prefix = "tencent")
public class CosClientTest implements InitializingBean {

    /**
     * 腾讯云的SecretId
     */
    @Value("${secretId}")
    private String secretId;

    /**
     * 腾讯云的SecretKey
     */
    @Value("${secretKey}")
    private String secretKey;

    /**
     * 腾讯云的bucket (存储桶)
     */
    @Value("${bucket}")
    private String bucket;

    /**
     * 腾讯云的region(bucket所在地区)
     */
    @Value("${region}")
    private String region;

    /**
     * 腾讯云的allowPrefix(允许上传的路径)
     */
    @Value("${allowPrefix}")
    private String allowPrefix;

    /**
     * 腾讯云的临时密钥时长(单位秒)
     */
    @Value("${durationSeconds}")
    private String durationSeconds;

    /**
     * 腾讯云的访问基础链接
     */
    @Value("${baseUrl}")
    private String baseUrl;

    @Override
    public void afterPropertiesSet() {
        System.out.println("================="+secretId);
    }

    /**
     * @param fullFileName 文件服务器下的根路径,即key,如: doc/picture.jpg
     * @param file
     * @return 成功返回文件路径, 失败返回null
     * @description: 使用临时秘钥上传文件
     * @author: Zhiwei Wang
     * @date: 2020/12/30 9:36
     */
    public String keyUploadFile(String fullFileName, File file) {
        //获取临时密钥
        JSONObject temp = getTempKey();
        // 用户基本信息:解析临时密钥中的相关信息
        String tmpSecretId = temp.getJSONObject("credentials").getString("tmpSecretId");
        String tmpSecretKey = temp.getJSONObject("credentials").getString("tmpSecretKey");
        String sessionToken = temp.getJSONObject("credentials").getString("sessionToken");

        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(tmpSecretId, tmpSecretKey);
        // 2 设置 bucket 区域
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        // 3 生成 cos 客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        // bucket名需包含appid
        String bucketName = bucket;
        // 上传 object, 建议 20M 以下的文件使用该接口
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fullFileName, file);
        // 设置 x-cos-security-token header 字段
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setSecurityToken(sessionToken);
        putObjectRequest.setMetadata(objectMetadata);
        String rtValue = null;
        try {
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
            // 成功:putobjectResult 会返回文件的 etag
            String etag = putObjectResult.getETag();
            rtValue = baseUrl + fullFileName;
        } catch (CosServiceException e) {
            //失败,抛出 CosServiceException
            e.printStackTrace();
        } catch (CosClientException e) {
            //失败,抛出 CosClientException
            e.printStackTrace();
        } finally {
            // 关闭客户端
            cosclient.shutdown();
            //返回文件的网络访问url
            return rtValue;
        }
    }

    private void keyDownloadFile() {
        //获取临时密钥
        JSONObject temp = getTempKey();
        // 用户基本信息:解析临时密钥中的相关信息
        String tmpSecretId = temp.getJSONObject("credentials").getString("tmpSecretId");
        String tmpSecretKey = temp.getJSONObject("credentials").getString("tmpSecretKey");
        String sessionToken = temp.getJSONObject("credentials").getString("sessionToken");

        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(tmpSecretId, tmpSecretKey);
        // 2 设置 bucket 区域
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        // 3 生成 cos 客户端
        COSClient cosclient = new COSClient(cred, clientConfig);

    }

    /**
     * 生成临时密钥
     *
     * @return
     */
    private JSONObject getTempKey() {
        TreeMap<String, Object> config = new TreeMap<String, Object>();
        try {//使用永久密钥生成临时密钥
            config.put("SecretId", secretId);
            config.put("SecretKey", secretKey);
            config.put("durationSeconds", Integer.parseInt(durationSeconds));
            config.put("bucket", bucket);
            config.put("region", region);
            config.put("allowPrefix", allowPrefix);
            //密钥的权限列表,其他权限列表请看
            //https://cloud.tencent.com/document/product/436/31923
//            String[] allowActions = new String[]{
//                    // 简单上传
//                    "name/cos:PutObject",
//                    // 表单上传、小程序上传
//                    "name/cos:PostObject",
//                    // 分片上传
//                    "name/cos:InitiateMultipartUpload",
//                    "name/cos:ListMultipartUploads",
//                    "name/cos:ListParts",
//                    "name/cos:UploadPart",
//                    "name/cos:CompleteMultipartUpload"
//            };
//            config.put("allowActions", allowActions);
            JSONObject credential = CosStsClient.getCredential(config);
            //成功返回临时密钥信息,如下打印密钥信息
            System.out.println(credential);
            return credential;
        } catch (Exception e) {
            //失败抛出异常
            throw new IllegalArgumentException("no valid secret !");
        }
    }

    /**
     * 初始化CosClient相关配置, appid、accessKey、secretKey、region
     *
     * @return
     */
    public COSClient getCosClient() {
        // 1 初始化用户身份信息(secretId, secretKey)。
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region regiona = new Region(region);
        ClientConfig clientConfig = new ClientConfig(regiona);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }

    /**
     * 上传文件
     *
     * @return
     */
    public String uploadFile(String fullFileName, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, fullFileName, file);

        // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
        putObjectRequest.setStorageClass(StorageClass.Standard_IA);
        COSClient cc = getCosClient();
        String etag = "";
        String resutl;
        try {
            PutObjectResult putObjectResult = cc.putObject(putObjectRequest);
            // putobjectResult会返回文件的etag
            etag = putObjectResult.getETag();
            resutl = baseUrl + fullFileName;
        } catch (CosServiceException e) {
            e.printStackTrace();

            resutl=e.getMessage();
        } catch (CosClientException e) {
            e.printStackTrace();
            resutl=e.getMessage();
        }finally {
            // 关闭客户端
            cc.shutdown();
        }
        return resutl;
    }

    /**
     * 下载文件
     *
     * @param bucketName
     * @param path
     * @return
     */
    public String downLoadFile(String bucketName, String path, String fullFileName) {
        File downFile = new File(fullFileName);
        COSClient cc = getCosClient();
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, path);

        ObjectMetadata downObjectMeta = cc.getObject(getObjectRequest, downFile);
        cc.shutdown();
        String etag = downObjectMeta.getETag();
        return etag;
    }

    /**
     * 删除文件
     *
     * @param bucketName
     * @param path
     */
    public void deleteFile(String bucketName, String path) {
        COSClient cc = getCosClient();
        try {
            cc.deleteObject(bucketName, path);
        } catch (CosClientException e) {
            e.printStackTrace();
        } finally {
            cc.shutdown();
        }

    }

    /**
     * 创建桶
     *
     * @param bucketName
     * @return
     * @throws CosClientException
     * @throws CosServiceException
     */
    public Bucket createBucket(String bucketName) throws CosClientException, CosServiceException {
        COSClient cc = getCosClient();
        Bucket bucket = null;
        try {
            bucket = cc.createBucket(bucketName);
        } catch (CosClientException e) {
            e.printStackTrace();
        } finally {
        }
        return bucket;
    }

    ;

    /**
     * 删除桶
     *
     * @param bucketName
     * @throws CosClientException
     * @throws CosServiceException
     */
    public void deleteBucket(String bucketName) throws CosClientException, CosServiceException {
        COSClient cc = getCosClient();
        try {
            cc.deleteBucket(bucketName);
        } catch (CosClientException e) {
            e.printStackTrace();
        } finally {
        }
    }

    ;

    /**
     * 判断桶是否存在
     *
     * @param bucketName
     * @return
     * @throws CosClientException
     * @throws CosServiceException
     */
    public boolean doesBucketExist(String bucketName) throws CosClientException, CosServiceException {
        COSClient cc = getCosClient();
        boolean bucketExistFlag = cc.doesBucketExist(bucketName);
        return bucketExistFlag;
    }

    ;

    /**
     * 查看桶文件
     *
     * @param bucketName
     * @return
     * @throws CosClientException
     * @throws CosServiceException
     */
    public ObjectListing listObjects(String bucketName) throws CosClientException, CosServiceException {
        COSClient cc = getCosClient();

        // 获取 bucket 下成员(设置 delimiter)
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
        listObjectsRequest.setBucketName(bucketName);
        // 设置 list 的 prefix, 表示 list 出来的文件 key 都是以这个 prefix 开始
        listObjectsRequest.setPrefix("");
        // 设置 delimiter 为/, 即获取的是直接成员,不包含目录下的递归子成员
        listObjectsRequest.setDelimiter("/");
        // 设置 marker, (marker 由上一次 list 获取到, 或者第一次 list marker 为空)
        listObjectsRequest.setMarker("");
        // 设置最多 list 100 个成员,(如果不设置, 默认为 1000 个,最大允许一次 list 1000 个 key)
        listObjectsRequest.setMaxKeys(100);

        ObjectListing objectListing = cc.listObjects(listObjectsRequest);
        // 获取下次 list 的 marker
        String nextMarker = objectListing.getNextMarker();
        // 判断是否已经 list 完, 如果 list 结束, 则 isTruncated 为 false, 否则为 true
        boolean isTruncated = objectListing.isTruncated();
        List<COSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
        for (COSObjectSummary cosObjectSummary : objectSummaries) {
            // get file path
            String key = cosObjectSummary.getKey();
            // get file length
            long fileSize = cosObjectSummary.getSize();
            // get file etag
            String eTag = cosObjectSummary.getETag();
            // get last modify time
            Date lastModified = cosObjectSummary.getLastModified();
            // get file save type
            String StorageClassStr = cosObjectSummary.getStorageClass();
        }
        return objectListing;
    }

    public void main(String[] args) {
        //uploadFile();
        //downLoadFile();
        //deleteFile();
        //createBucket();
        //deleteBucket();
        //doesBucketExist();
        //listObjects();
    }

}

           

springboot配置文件

tencent:
  # 腾讯云的SecretId(永久的,可在控制台开启或关闭)
  secretId: ***
  # 腾讯云的SecretKey(永久的,可在控制台开启或关闭)
  secretKey: ***
  # 腾讯云的allowPrefix(允许上传的路径)
  allowPrefix: '*'
  # 腾讯云的访问基础链接:
  baseUrl: https://xingchen-***.cos.ap-chengdu.myqcloud.com/
  # 腾讯云的bucket (存储桶)
  bucket: xingchen-***
  # 腾讯云的临时密钥时长(单位秒)
  durationSeconds: 1800
  # 腾讯云的region(bucket所在地区)
  region: ap-chengdu