Search in sources :

Example 1 with Region

use of com.qcloud.cos.region.Region in project halo by ruibaby.

the class TencentCosFileHandler method delete.

@Override
public void delete(String key) {
    Assert.notNull(key, "File key must not be blank");
    // Get config
    String region = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_REGION).toString();
    String secretId = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_ID).toString();
    String secretKey = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_KEY).toString();
    String bucketName = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_BUCKET_NAME).toString();
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    Region regionConfig = new Region(region);
    ClientConfig clientConfig = new ClientConfig(regionConfig);
    // Init OSS client
    COSClient cosClient = new COSClient(cred, clientConfig);
    try {
        cosClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        throw new FileOperationException("附件 " + key + " 从腾讯云删除失败", e);
    } finally {
        cosClient.shutdown();
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) FileOperationException(run.halo.app.exception.FileOperationException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) FileOperationException(run.halo.app.exception.FileOperationException)

Example 2 with Region

use of com.qcloud.cos.region.Region in project litemall by linlinjava.

the class TencentStorage method getCOSClient.

private COSClient getCOSClient() {
    if (cosClient == null) {
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        cosClient = new COSClient(cred, clientConfig);
    }
    return cosClient;
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 3 with Region

use of com.qcloud.cos.region.Region in project e3mall by colg-cloud.

the class TencentCosTest method testName.

@Test
public void testName() {
    COSCredentials cred = new BasicCOSCredentials("AKIDUvjV6VEvvjS4Vliw360iEYpvpgMrqMKF", "sY3NVSL8kLk8KK8lIftzud9VggU5Vkne");
    // 采用了新的region名字,可用region的列表可以在官网文档中获取,也可以参考下面的XML SDK和JSON SDK的地域对照表
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket的名字需要的包含appId
    String bucketName = "colg-1256242877";
    // 上传文件
    File localFile = new File(PROJECT_PATH + "\\src\\test\\resources\\images\\FastDfs架构.png");
    String key = "colg.png";
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
    putObjectRequest.setStorageClass(StorageClass.Standard);
    PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
    // putObjectResult会返回文件的etag
    String etag = putObjectResult.getETag();
    log.info("etag: {}", etag);
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) BaseTest(cn.e3mall.common.tencent.cos.BaseTest) Test(org.junit.Test)

Example 4 with Region

use of com.qcloud.cos.region.Region in project yili-music by programmer-yili.

the class CosStorageServiceImpl method createCOSClient.

// Todo: 改造client获取方法
COSClient createCOSClient() {
    // 这里需要已经获取到临时密钥的结果。
    // 临时密钥的生成参考 https://cloud.tencent.com/document/product/436/14048#cos-sts-sdk
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    // ClientConfig 中包含了后续请求 COS 的客户端设置:
    ClientConfig clientConfig = new ClientConfig();
    // 设置 bucket 的地域
    // COS_REGION 请参照 https://cloud.tencent.com/document/product/436/6224
    clientConfig.setRegion(new Region(region));
    // 设置请求协议, http 或者 https
    // 5.6.53 及更低的版本,建议设置使用 https 协议
    // 5.6.54 及更高版本,默认使用了 https
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 以下的设置,是可选的:
    // 设置 socket 读取超时,默认 30s
    clientConfig.setSocketTimeout(30 * 1000);
    // 设置建立连接超时,默认 30s
    clientConfig.setConnectionTimeout(30 * 1000);
    return new COSClient(cred, clientConfig);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 5 with Region

use of com.qcloud.cos.region.Region in project RuleApi by buxia97.

the class UploadController method cosUpload.

/**
 * 上传道腾讯云服务器(https://cloud.tencent.com/document/product/436/10199)
 * @return
 */
@RequestMapping(value = "/cosUpload", method = RequestMethod.POST)
@ResponseBody
public Object cosUpload(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "token", required = false) String token) throws IOException {
    Integer uStatus = UStatus.getStatus(token, this.dataprefix, redisTemplate);
    if (uStatus == 0) {
        return Result.getResultJson(0, "用户未登录或Token验证失败", null);
    }
    if (file == null) {
        return new UploadMsg(0, "文件为空", null);
    }
    String oldFileName = file.getOriginalFilename();
    String eName = oldFileName.substring(oldFileName.lastIndexOf("."));
    // 检查是否是图片
    BufferedImage bi = ImageIO.read(file.getInputStream());
    if (bi == null) {
        return Result.getResultJson(0, "请上传图片文件", null);
    }
    String newFileName = UUID.randomUUID() + eName;
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DATE);
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
    // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region(bucket));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
    String bucketName = this.bucketName;
    // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
    // 大文件上传请参照 API 文档高级 API 上传
    File localFile = null;
    try {
        localFile = File.createTempFile("temp", null);
        file.transferTo(localFile);
        // 指定要上传到 COS 上的路径
        String key = "/" + this.prefix + "/" + year + "/" + month + "/" + day + "/" + newFileName;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        // return new UploadMsg(1,"上传成功",this.path + putObjectRequest.getKey());
        Map<String, String> info = new HashMap<String, String>();
        info.put("url", this.path + putObjectRequest.getKey());
        return Result.getResultJson(1, "上传成功", info);
    } catch (IOException e) {
        return Result.getResultJson(1, "上传失败", null);
    } finally {
        // 关闭客户端(关闭后台线程)
        cosclient.shutdown();
    }
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) HashMap(java.util.HashMap) Calendar(java.util.Calendar) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) COSClient(com.qcloud.cos.COSClient) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Region (com.qcloud.cos.region.Region)116 COSCredentials (com.qcloud.cos.auth.COSCredentials)100 ClientConfig (com.qcloud.cos.ClientConfig)98 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)96 COSClient (com.qcloud.cos.COSClient)94 CosClientException (com.qcloud.cos.exception.CosClientException)39 CosServiceException (com.qcloud.cos.exception.CosServiceException)37 File (java.io.File)24 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)14 PutObjectResult (com.qcloud.cos.model.PutObjectResult)14 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)13 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)11 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)8 Copy (com.qcloud.cos.transfer.Copy)8 Test (org.junit.Test)8 CopyResult (com.qcloud.cos.model.CopyResult)7 LinkedList (java.util.LinkedList)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)6