Search in sources :

Example 81 with CosClientException

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

the class MultipartUploadDemo method listPartDemo.

// list part用于获取已上传的分片, 如果已上传的分片数量较多, 需要循环多次调用list part获取已上传的所有的分片
public static List<PartETag> listPartDemo(String uploadId) {
    // 1 初始化用户身份信息(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-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    // uploadid(通过initiateMultipartUpload或者ListMultipartUploads获取)
    // 用于保存已上传的分片信息
    List<PartETag> partETags = new LinkedList<>();
    PartListing partListing = null;
    ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
    do {
        try {
            partListing = cosclient.listParts(listPartsRequest);
        } catch (CosServiceException e) {
            throw e;
        } catch (CosClientException e) {
            throw e;
        }
        for (PartSummary partSummary : partListing.getParts()) {
            partETags.add(new PartETag(partSummary.getPartNumber(), partSummary.getETag()));
        }
        listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker());
    } while (partListing.isTruncated());
    cosclient.shutdown();
    return partETags;
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) PartETag(com.qcloud.cos.model.PartETag) LinkedList(java.util.LinkedList) PartListing(com.qcloud.cos.model.PartListing) COSClient(com.qcloud.cos.COSClient) ListPartsRequest(com.qcloud.cos.model.ListPartsRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) PartSummary(com.qcloud.cos.model.PartSummary)

Example 82 with CosClientException

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

the class MultipartUploadDemo method abortPartUploadDemo.

// 终止分块上传
public static void abortPartUploadDemo(String uploadId) {
    // 1 初始化用户身份信息(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-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    // uploadid(通过initiateMultipartUpload或者ListMultipartUploads获取)
    AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, uploadId);
    try {
        cosclient.abortMultipartUpload(abortMultipartUploadRequest);
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) AbortMultipartUploadRequest(com.qcloud.cos.model.AbortMultipartUploadRequest) ClientConfig(com.qcloud.cos.ClientConfig)

Example 83 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project simpleFS by shengdingbox.

the class QCloudOssApiClient method fileList.

@Override
public List<VirtualFile> fileList(FileListRequesr fileListRequesr) {
    List<VirtualFile> virtualFiles = new ArrayList<>();
    // 指定返回结果使用URL编码,则您需要对结果中的prefix、delemiter、startAfter、key和commonPrefix进行URL解码。
    ObjectListing objectListing = null;
    do {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
        // 设置 bucket 名称
        listObjectsRequest.setBucketName(bucketName);
        // 设置列出的对象名以 prefix 为前缀
        listObjectsRequest.setPrefix(fileListRequesr.getPrefix());
        // 设置最大列出多少个对象, 一次 listobject 最大支持1000
        listObjectsRequest.setMaxKeys(fileListRequesr.getSize());
        try {
            objectListing = cosClient.listObjects(listObjectsRequest);
        } catch (CosClientException e) {
            e.printStackTrace();
        }
        // 文件名称解码。
        for (COSObjectSummary s : objectListing.getObjectSummaries()) {
            String decodedKey = null;
            try {
                decodedKey = URLDecoder.decode(s.getKey(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            VirtualFile virtualFile = VirtualFile.builder().originalFileName(decodedKey).suffix(this.suffix).uploadStartTime(s.getLastModified()).uploadEndTime(s.getLastModified()).filePath(this.newFileName).size(s.getSize()).fileHash(s.getETag()).fullFilePath(decodedKey).build();
            virtualFiles.add(virtualFile);
        }
        String nextContinuationToken = objectListing.getNextMarker();
    } while (objectListing.isTruncated());
    // 确认本进程不再使用 cosClient 实例之后,关闭之
    cosClient.shutdown();
    return virtualFiles;
}
Also used : VirtualFile(com.zhouzifei.tool.dto.VirtualFile) CosClientException(com.qcloud.cos.exception.CosClientException) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 84 with CosClientException

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

the class COSCryptoModuleAE method adjustToDesiredRange.

/**
 * Adjusts the retrieved COSObject so that the object contents contain only the range of bytes
 * desired by the user. Since encrypted contents can only be retrieved in CIPHER_BLOCK_SIZE (16
 * bytes) chunks, the COSObject potentially contains more bytes than desired, so this method
 * adjusts the contents range.
 *
 * @param cosObject The COSObject retrieved from COS that could possibly contain more bytes than
 *        desired by the user.
 * @param range A two-element array of longs corresponding to the start and finish (inclusive)
 *        of a desired range of bytes.
 * @param instruction Instruction file in JSON or null if no instruction file is involved
 * @return The COSObject with adjusted object contents containing only the range desired by the
 *         user. If the range specified is invalid, then the COSObject is returned without any
 *         modifications.
 */
protected final COSObjectWrapper adjustToDesiredRange(COSObjectWrapper cosObject, long[] range, Map<String, String> instruction) {
    if (range == null)
        return cosObject;
    // Figure out the original encryption scheme used, which can be
    // different from the crypto scheme used for decryption.
    ContentCryptoScheme encryptionScheme = cosObject.encryptionSchemeOf(instruction);
    // range get on data encrypted using AES_GCM
    final long instanceLen = cosObject.getObjectMetadata().getInstanceLength();
    final long maxOffset = instanceLen - encryptionScheme.getTagLengthInBits() / 8 - 1;
    if (range[1] > maxOffset) {
        range[1] = maxOffset;
        if (range[0] > range[1]) {
            // Return empty content
            // First let's close the existing input stream to avoid resource
            // leakage
            IOUtils.closeQuietly(cosObject.getObjectContent(), log);
            cosObject.setObjectContent(new ByteArrayInputStream(new byte[0]));
            return cosObject;
        }
    }
    if (range[0] > range[1]) {
        // Make no modifications if range is invalid.
        return cosObject;
    }
    try {
        COSObjectInputStream objectContent = cosObject.getObjectContent();
        InputStream adjustedRangeContents = new AdjustedRangeInputStream(objectContent, range[0], range[1]);
        cosObject.setObjectContent(new COSObjectInputStream(adjustedRangeContents, objectContent.getHttpRequest()));
        return cosObject;
    } catch (IOException e) {
        throw new CosClientException("Error adjusting output to desired byte range: " + e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) COSObjectInputStream(com.qcloud.cos.model.COSObjectInputStream) InputStream(java.io.InputStream) CosClientException(com.qcloud.cos.exception.CosClientException) COSObjectInputStream(com.qcloud.cos.model.COSObjectInputStream) IOException(java.io.IOException)

Example 85 with CosClientException

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

the class COSCryptoModuleAE method decipherWithInstFileSuffix.

/**
 * Same as {@link #decipher(GetObjectRequest, long[], long[], COSObject)} but makes use of an
 * instruction file with the specified suffix.
 *
 * @param instFileSuffix never null or empty (which is assumed to have been sanitized upstream.)
 */
private COSObject decipherWithInstFileSuffix(GetObjectRequest req, long[] desiredRange, long[] cryptoRange, COSObject retrieved, String instFileSuffix) {
    final COSObjectId id = req.getCOSObjectId();
    // Check if encrypted info is in an instruction file
    final COSObjectWrapper ifile = fetchInstructionFile(id, instFileSuffix);
    if (ifile == null) {
        throw new CosClientException("Instruction file with suffix " + instFileSuffix + " is not found for " + retrieved);
    }
    try {
        return decipherWithInstructionFile(req, desiredRange, cryptoRange, new COSObjectWrapper(retrieved, id), ifile);
    } finally {
        IOUtils.closeQuietly(ifile, log);
    }
}
Also used : COSObjectId(com.qcloud.cos.model.COSObjectId) CosClientException(com.qcloud.cos.exception.CosClientException)

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