Search in sources :

Example 66 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class ContentCryptoScheme method createCipherLite.

/**
 * Creates and initializes a {@link CipherLite} for content
 * encrypt/decryption.
 *
 * @param cek
 *            content encrypting key
 * @param iv
 *            initialization vector
 * @param cipherMode
 *            such as {@link Cipher#ENCRYPT_MODE}
 * @param securityProvider
 *            optional security provider to be used but only if there is no
 *            specific provider defined for the specified scheme.
 * @return the cipher lite created and initialized.
 */
CipherLite createCipherLite(SecretKey cek, byte[] iv, int cipherMode, Provider securityProvider) {
    String specificProvider = getSpecificCipherProvider();
    Cipher cipher;
    try {
        if (specificProvider != null) {
            // use the specific provider if defined
            cipher = Cipher.getInstance(getCipherAlgorithm(), specificProvider);
        } else if (securityProvider != null) {
            // use the one optionally specified in the input
            cipher = Cipher.getInstance(getCipherAlgorithm(), securityProvider);
        } else {
            // use the default provider
            cipher = Cipher.getInstance(getCipherAlgorithm());
        }
        cipher.init(cipherMode, cek, new IvParameterSpec(iv));
        return newCipherLite(cipher, cek, cipherMode);
    } catch (Exception e) {
        throw e instanceof RuntimeException ? (RuntimeException) e : new CosClientException("Unable to build cipher: " + e.getMessage() + "\nMake sure you have the JCE unlimited strength policy files installed and " + "configured for your JVM", e);
    }
}
Also used : CosClientException(com.qcloud.cos.exception.CosClientException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) CosClientException(com.qcloud.cos.exception.CosClientException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException)

Example 67 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class CompleteMultipartUpload method collectPartETags.

/**
 * Collects the Part ETags for initiating the complete multi-part upload
 * request. This is blocking as it waits until all the upload part threads
 * complete.
 */
private List<PartETag> collectPartETags() {
    final List<PartETag> partETags = new ArrayList<PartETag>();
    partETags.addAll(eTagsBeforeResume);
    for (Future<PartETag> future : futures) {
        try {
            partETags.add(future.get());
        } catch (Exception e) {
            throw new CosClientException("Unable to complete multi-part upload. Individual part upload failed : " + e.getCause().getMessage(), e.getCause());
        }
    }
    return partETags;
}
Also used : CosClientException(com.qcloud.cos.exception.CosClientException) ArrayList(java.util.ArrayList) PartETag(com.qcloud.cos.model.PartETag) CosClientException(com.qcloud.cos.exception.CosClientException)

Example 68 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class DownloadCallable method retryableDownloadCOSObjectToFile.

private COSObject retryableDownloadCOSObjectToFile(File file, RetryableCOSDownloadTask retryableCOSDownloadTask, boolean appendData) {
    boolean hasRetried = false;
    COSObject cosObject;
    for (; ; ) {
        if (resumeExistingDownload && hasRetried) {
            // Need to adjust the get range or else we risk corrupting the downloaded file
            adjustRequest(req);
        }
        cosObject = retryableCOSDownloadTask.getCOSObjectStream();
        if (cosObject == null)
            return null;
        try {
            if (testing && resumeExistingDownload && !hasRetried) {
                throw new CosClientException("testing");
            }
            ServiceUtils.downloadToFile(cosObject, file, retryableCOSDownloadTask.needIntegrityCheck(), appendData, expectedFileLength);
            return cosObject;
        } catch (CosClientException ace) {
            if (!ace.isRetryable())
                throw ace;
            // The current code will retry the download only when case 2) or 3) happens.
            if (ace.getCause() instanceof SocketException || ace.getCause() instanceof SSLProtocolException) {
                throw ace;
            } else {
                if (hasRetried)
                    throw ace;
                else {
                    log.info("Retry the download of object " + cosObject.getKey() + " (bucket " + cosObject.getBucketName() + ")", ace);
                    hasRetried = true;
                }
            }
        } finally {
            cosObject.getObjectContent().abort();
        }
    }
}
Also used : SSLProtocolException(javax.net.ssl.SSLProtocolException) SocketException(java.net.SocketException) COSObject(com.qcloud.cos.model.COSObject) CosClientException(com.qcloud.cos.exception.CosClientException)

Example 69 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class KMSEncryptionClientDemo method transferManagerDemo.

static void transferManagerDemo() {
    ExecutorService threadPool = Executors.newFixedThreadPool(32);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosClient, threadPool);
    TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
    transferManagerConfiguration.setMultipartUploadThreshold(1024 * 1024);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, bigLocalFile);
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
        Upload upload = transferManager.upload(putObjectRequest);
        UploadResult uploadResult = upload.waitForUploadResult();
        System.out.println(uploadResult.getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待download结束, 成功返回DownloadResult, 失败抛出异常.
        Download download = transferManager.download(getObjectRequest, new File("downLen10m.txt"));
        download.waitForCompletion();
        System.out.println(download.getObjectMetadata().getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    transferManager.shutdownNow();
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) TransferManagerConfiguration(com.qcloud.cos.transfer.TransferManagerConfiguration) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) ExecutorService(java.util.concurrent.ExecutorService) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Download(com.qcloud.cos.transfer.Download) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 70 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class ListObjectsDemo method listObjectsDemo.

public static void listObjectsDemo() {
    // 1 初始化用户身份信息(appid, secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名称, 需包含appid
    String bucketName = "mybucket-1251668577";
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
    // 设置bucket名称
    listObjectsRequest.setBucketName(bucketName);
    // prefix表示列出的object的key以prefix开始
    listObjectsRequest.setPrefix("");
    // 设置最大遍历出多少个对象, 一次listobject最大支持1000
    listObjectsRequest.setMaxKeys(1000);
    // listObjectsRequest.setDelimiter("/");
    ObjectListing objectListing = null;
    try {
        objectListing = cosclient.listObjects(listObjectsRequest);
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // common prefix表示表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径
    List<String> commonPrefixs = objectListing.getCommonPrefixes();
    // object summary表示所有列出的object列表
    List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
    for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {
        // 文件的路径key
        String key = cosObjectSummary.getKey();
        // 文件的etag
        String etag = cosObjectSummary.getETag();
        // 文件的长度
        long fileSize = cosObjectSummary.getSize();
        // 文件的存储类型
        String storageClasses = cosObjectSummary.getStorageClass();
        System.out.println("key: " + key);
    }
    cosclient.shutdown();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) COSObjectSummary(com.qcloud.cos.model.COSObjectSummary) CosClientException(com.qcloud.cos.exception.CosClientException) ObjectListing(com.qcloud.cos.model.ObjectListing) COSClient(com.qcloud.cos.COSClient) ListObjectsRequest(com.qcloud.cos.model.ListObjectsRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Aggregations

CosClientException (com.qcloud.cos.exception.CosClientException)111 CosServiceException (com.qcloud.cos.exception.CosServiceException)64 COSCredentials (com.qcloud.cos.auth.COSCredentials)41 ClientConfig (com.qcloud.cos.ClientConfig)39 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)39 Region (com.qcloud.cos.region.Region)39 COSClient (com.qcloud.cos.COSClient)37 IOException (java.io.IOException)31 File (java.io.File)28 ByteArrayInputStream (java.io.ByteArrayInputStream)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 TransferManager (com.qcloud.cos.transfer.TransferManager)14 ExecutorService (java.util.concurrent.ExecutorService)14 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)13 URISyntaxException (java.net.URISyntaxException)13 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)12 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 SecretKey (javax.crypto.SecretKey)12 MalformedURLException (java.net.MalformedURLException)11 PutObjectResult (com.qcloud.cos.model.PutObjectResult)10