Search in sources :

Example 61 with COSCredentials

use of com.qcloud.cos.auth.COSCredentials in project roof-im by madfroglx.

the class OCSTest method createAuthorization.

@Test
public void createAuthorization() throws UnsupportedEncodingException {
    String appid = "1255710173";
    String secret_id = "AKIDSpCPQFhNfmmcJ0Q0FKE7pNjuuiKlN73o";
    String secret_key = "kKjOsEPOCoFUlwviLm27IJ9nFWcaNyOc";
    // String sessionToken = "562af6ffcbe981dd21bd7434176dcbb2f883839030001";
    // 设置秘钥
    COSCredentials cred = new BasicCOSCredentials(appid, secret_id, secret_key);
    COSSigner cosSigner = new COSSigner();
    System.out.println(cosSigner.buildAuthorizationStr(HttpMethodName.POST, "/", cred, new Date(1520516362000L)));
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) COSSigner(com.qcloud.cos.auth.COSSigner) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Date(java.util.Date) Test(org.junit.Test)

Example 62 with COSCredentials

use of com.qcloud.cos.auth.COSCredentials in project halo by ruibaby.

the class TencentCosFileHandler method upload.

@Override
public UploadResult upload(MultipartFile file) {
    Assert.notNull(file, "Multipart file must not be null");
    // Get config
    String protocol = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_PROTOCOL).toString();
    String domain = optionService.getByPropertyOrDefault(TencentCosProperties.COS_DOMAIN, String.class, "");
    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();
    String source = optionService.getByPropertyOrDefault(TencentCosProperties.COS_SOURCE, String.class, "");
    String styleRule = optionService.getByPropertyOrDefault(TencentCosProperties.COS_STYLE_RULE, String.class, "");
    String thumbnailStyleRule = optionService.getByPropertyOrDefault(TencentCosProperties.COS_THUMBNAIL_STYLE_RULE, String.class, "");
    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);
    StringBuilder basePath = new StringBuilder(protocol);
    if (StringUtils.isNotEmpty(domain)) {
        basePath.append(domain).append(URL_SEPARATOR);
    } else {
        basePath.append(bucketName).append(".cos.").append(region).append(".myqcloud.com").append(URL_SEPARATOR);
    }
    try {
        FilePathDescriptor pathDescriptor = new FilePathDescriptor.Builder().setBasePath(basePath.toString()).setSubPath(source).setAutomaticRename(true).setRenamePredicate(relativePath -> attachmentRepository.countByFileKeyAndType(relativePath, AttachmentType.TENCENTCOS) > 0).setOriginalName(file.getOriginalFilename()).build();
        // Upload
        ObjectMetadata objectMetadata = new ObjectMetadata();
        // 提前告知输入流的长度, 否则可能导致 oom
        objectMetadata.setContentLength(file.getSize());
        // 设置 Content type, 默认是 application/octet-stream
        objectMetadata.setContentType(file.getContentType());
        PutObjectResult putObjectResponseFromInputStream = cosClient.putObject(bucketName, pathDescriptor.getRelativePath(), file.getInputStream(), objectMetadata);
        if (putObjectResponseFromInputStream == null) {
            throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到腾讯云失败 ");
        }
        String fullPath = pathDescriptor.getFullPath();
        // Response result
        UploadResult uploadResult = new UploadResult();
        uploadResult.setFilename(pathDescriptor.getName());
        uploadResult.setFilePath(StringUtils.isBlank(styleRule) ? fullPath : fullPath + styleRule);
        uploadResult.setKey(pathDescriptor.getRelativePath());
        uploadResult.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
        uploadResult.setSuffix(pathDescriptor.getExtension());
        uploadResult.setSize(file.getSize());
        // Handle thumbnail
        handleImageMetadata(file, uploadResult, () -> {
            if (ImageUtils.EXTENSION_ICO.equals(pathDescriptor.getExtension())) {
                uploadResult.setThumbPath(fullPath);
                return fullPath;
            } else {
                return StringUtils.isBlank(thumbnailStyleRule) ? fullPath : fullPath + thumbnailStyleRule;
            }
        });
        return uploadResult;
    } catch (Exception e) {
        throw new FileOperationException("附件 " + file.getOriginalFilename() + " 上传失败(腾讯云)", e);
    } 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) FileOperationException(run.halo.app.exception.FileOperationException) FileOperationException(run.halo.app.exception.FileOperationException) COSClient(com.qcloud.cos.COSClient) Region(com.qcloud.cos.region.Region) UploadResult(run.halo.app.model.support.UploadResult) ClientConfig(com.qcloud.cos.ClientConfig) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 63 with COSCredentials

use of com.qcloud.cos.auth.COSCredentials in project halo by halo-dev.

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 64 with COSCredentials

use of com.qcloud.cos.auth.COSCredentials in project cos-java-sdk-v5 by tencentyun.

the class COSClient method invoke.

private <X, Y extends CosServiceRequest> X invoke(CosHttpRequest<Y> request, HttpResponseHandler<CosServiceResponse<X>> responseHandler) throws CosClientException, CosServiceException {
    COSSigner cosSigner = clientConfig.getCosSigner();
    COSCredentials cosCredentials;
    CosServiceRequest cosServiceRequest = request.getOriginalRequest();
    if (cosServiceRequest != null && cosServiceRequest.getCosCredentials() != null) {
        cosCredentials = cosServiceRequest.getCosCredentials();
    } else {
        cosCredentials = fetchCredential();
    }
    Date expiredTime = new Date(System.currentTimeMillis() + clientConfig.getSignExpired() * 1000);
    boolean isCIWorkflowRequest = cosServiceRequest instanceof CIWorkflowServiceRequest;
    cosSigner.setCIWorkflowRequest(isCIWorkflowRequest);
    cosSigner.sign(request, cosCredentials, expiredTime);
    return this.cosHttpClient.exeute(request, responseHandler);
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) COSSigner(com.qcloud.cos.auth.COSSigner) CosServiceRequest(com.qcloud.cos.internal.CosServiceRequest) CIWorkflowServiceRequest(com.qcloud.cos.internal.CIWorkflowServiceRequest) Date(java.util.Date)

Example 65 with COSCredentials

use of com.qcloud.cos.auth.COSCredentials in project cos-java-sdk-v5 by tencentyun.

the class PutGetDelTest method testRequestSpecifiedTmpKeyInfoPutGetDel.

@Test
public void testRequestSpecifiedTmpKeyInfoPutGetDel() throws CosServiceException, IOException, InterruptedException {
    COSClient cosclient = buildTemporyCredentialsCOSClient(1800L);
    File localFile = buildTestFile(1024L);
    COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey);
    try {
        String key = "ut/request-specified-key";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
        putObjectRequest.setCosCredentials(cosCredentials);
        cosclient.putObject(putObjectRequest);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        getObjectRequest.setCosCredentials(cosCredentials);
        cosclient.getObject(getObjectRequest);
        DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket, key);
        deleteObjectRequest.setCosCredentials(cosCredentials);
        cosclient.deleteObject(deleteObjectRequest);
    } finally {
        localFile.delete();
        cosclient.shutdown();
    }
}
Also used : DeleteObjectRequest(com.qcloud.cos.model.DeleteObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Aggregations

COSCredentials (com.qcloud.cos.auth.COSCredentials)112 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)105 Region (com.qcloud.cos.region.Region)105 ClientConfig (com.qcloud.cos.ClientConfig)99 COSClient (com.qcloud.cos.COSClient)96 CosClientException (com.qcloud.cos.exception.CosClientException)42 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)23 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)12 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)11 PutObjectResult (com.qcloud.cos.model.PutObjectResult)11 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)10 Copy (com.qcloud.cos.transfer.Copy)8 CopyResult (com.qcloud.cos.model.CopyResult)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)7 Date (java.util.Date)7 LinkedList (java.util.LinkedList)7