Search in sources :

Example 86 with CosClientException

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

the class COSCryptoModuleAE method getObjectSecurely.

@Override
public ObjectMetadata getObjectSecurely(GetObjectRequest getObjectRequest, File destinationFile) {
    assertParameterNotNull(destinationFile, "The destination file parameter must be specified when downloading an object directly to a file");
    COSObject cosObject = getObjectSecurely(getObjectRequest);
    // getObject can return null if constraints were specified but not met
    if (cosObject == null)
        return null;
    OutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(destinationFile));
        byte[] buffer = new byte[1024 * 10];
        int bytesRead;
        while ((bytesRead = cosObject.getObjectContent().read(buffer)) > -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        throw new CosClientException("Unable to store object contents to disk: " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(outputStream, log);
        IOUtils.closeQuietly(cosObject.getObjectContent(), log);
    }
    return cosObject.getObjectMetadata();
}
Also used : COSObject(com.qcloud.cos.model.COSObject) CosClientException(com.qcloud.cos.exception.CosClientException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 87 with CosClientException

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

the class COSCryptoModuleBase method uploadPartSecurely.

/**
 * {@inheritDoc}
 *
 * <p>
 * <b>NOTE:</b> Because the encryption process requires context from previous blocks, parts
 * uploaded with the COSEncryptionClient (as opposed to the normal COSClient) must be
 * uploaded serially, and in order. Otherwise, the previous encryption context isn't available
 * to use when encrypting the current part.
 */
@Override
public UploadPartResult uploadPartSecurely(UploadPartRequest req) {
    final int blockSize = contentCryptoScheme.getBlockSizeInBytes();
    final boolean isLastPart = req.isLastPart();
    final String uploadId = req.getUploadId();
    final long partSize = req.getPartSize();
    final boolean partSizeMultipleOfCipherBlockSize = 0 == (partSize % blockSize);
    if (!isLastPart && !partSizeMultipleOfCipherBlockSize) {
        throw new CosClientException("Invalid part size: part sizes for encrypted multipart uploads must be multiples " + "of the cipher block size (" + blockSize + ") with the exception of the last part.");
    }
    final MultipartUploadCryptoContext uploadContext = multipartUploadContexts.get(uploadId);
    if (uploadContext == null) {
        throw new CosClientException("No client-side information available on upload ID " + uploadId);
    }
    final UploadPartResult result;
    // Checks the parts are uploaded in series
    uploadContext.beginPartUpload(req.getPartNumber());
    CipherLite cipherLite = cipherLiteForNextPart(uploadContext);
    final File fileOrig = req.getFile();
    final InputStream isOrig = req.getInputStream();
    SdkFilterInputStream isCurr = null;
    try {
        CipherLiteInputStream clis = newMultipartCOSCipherInputStream(req, cipherLite);
        // so the clis will be closed (in the finally block below) upon
        isCurr = clis;
        // unexpected failure should we opened a file undereath
        req.setInputStream(isCurr);
        // Treat all encryption requests as input stream upload requests,
        // not as file upload requests.
        req.setFile(null);
        req.setFileOffset(0);
        // 16-byte mac
        if (isLastPart) {
            // We only change the size of the last part
            long lastPartSize = computeLastPartSize(req);
            if (lastPartSize > -1)
                req.setPartSize(lastPartSize);
            if (uploadContext.hasFinalPartBeenSeen()) {
                throw new CosClientException("This part was specified as the last part in a multipart upload, but a previous part was already marked as the last part.  " + "Only the last part of the upload should be marked as the last part.");
            }
        }
        result = cos.uploadPart(req);
    } finally {
        cleanupDataSource(req, fileOrig, isOrig, isCurr, log);
        uploadContext.endPartUpload();
    }
    if (isLastPart)
        uploadContext.setHasFinalPartBeenSeen(true);
    return result;
}
Also used : UploadPartResult(com.qcloud.cos.model.UploadPartResult) CosClientException(com.qcloud.cos.exception.CosClientException) ByteArrayInputStream(java.io.ByteArrayInputStream) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) ReleasableInputStream(com.qcloud.cos.internal.ReleasableInputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) InputStream(java.io.InputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) InstructionFile(com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile) File(java.io.File)

Example 88 with CosClientException

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

the class COSCryptoModuleBase method createContentCryptoMaterial.

/**
 * Creates and returns a non-null content crypto material for the given request.
 *
 * @throws CosClientException if no encryption material can be found.
 */
protected final ContentCryptoMaterial createContentCryptoMaterial(CosServiceRequest req) {
    if (req instanceof EncryptionMaterialsFactory) {
        // per request level encryption materials
        EncryptionMaterialsFactory f = (EncryptionMaterialsFactory) req;
        final EncryptionMaterials materials = f.getEncryptionMaterials();
        if (materials != null) {
            return buildContentCryptoMaterial(materials, cryptoConfig.getCryptoProvider(), req);
        }
    }
    if (req instanceof MaterialsDescriptionProvider) {
        // per request level material description
        MaterialsDescriptionProvider mdp = (MaterialsDescriptionProvider) req;
        Map<String, String> matdesc_req = mdp.getMaterialsDescription();
        ContentCryptoMaterial ccm = newContentCryptoMaterial(kekMaterialsProvider, matdesc_req, cryptoConfig.getCryptoProvider(), req);
        if (ccm != null)
            return ccm;
        if (matdesc_req != null) {
            // check to see if KMS is in use and if so we should fall thru
            // to the COS client level encryption material
            EncryptionMaterials material = kekMaterialsProvider.getEncryptionMaterials();
            if (!material.isKMSEnabled()) {
                throw new CosClientException("No material available from the encryption material provider for description " + matdesc_req);
            }
        }
    // if there is no material description, fall thru to use
    // the per cos client level encryption materials
    }
    // per cos client level encryption materials
    return newContentCryptoMaterial(this.kekMaterialsProvider, cryptoConfig.getCryptoProvider(), req);
}
Also used : MaterialsDescriptionProvider(com.qcloud.cos.model.MaterialsDescriptionProvider) CosClientException(com.qcloud.cos.exception.CosClientException)

Example 89 with CosClientException

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

the class COSCryptoModuleBase method newMultipartCOSCipherInputStream.

protected final CipherLiteInputStream newMultipartCOSCipherInputStream(UploadPartRequest req, CipherLite cipherLite) {
    final File fileOrig = req.getFile();
    final InputStream isOrig = req.getInputStream();
    InputStream isCurr = null;
    try {
        if (fileOrig == null) {
            if (isOrig == null) {
                throw new IllegalArgumentException("A File or InputStream must be specified when uploading part");
            }
            isCurr = isOrig;
        } else {
            isCurr = new ResettableInputStream(fileOrig);
        }
        isCurr = new InputSubstream(isCurr, req.getFileOffset(), req.getPartSize(), req.isLastPart());
        return cipherLite.markSupported() ? new CipherLiteInputStream(isCurr, cipherLite, DEFAULT_BUFFER_SIZE, IS_MULTI_PART, req.isLastPart()) : new RenewableCipherLiteInputStream(isCurr, cipherLite, DEFAULT_BUFFER_SIZE, IS_MULTI_PART, req.isLastPart());
    } catch (Exception e) {
        cleanupDataSource(req, fileOrig, isOrig, isCurr, log);
        throw new CosClientException("Unable to create cipher input stream", e);
    }
}
Also used : InputSubstream(com.qcloud.cos.internal.InputSubstream) ByteArrayInputStream(java.io.ByteArrayInputStream) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) ReleasableInputStream(com.qcloud.cos.internal.ReleasableInputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) InputStream(java.io.InputStream) CosClientException(com.qcloud.cos.exception.CosClientException) InstructionFile(com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile) File(java.io.File) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 90 with CosClientException

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

the class DefaultCosHttpClient method handlerErrorMessage.

private <X extends CosServiceRequest> CosServiceException handlerErrorMessage(CosHttpRequest<X> request, HttpRequestBase httpRequestBase, final org.apache.http.HttpResponse apacheHttpResponse) throws IOException {
    final StatusLine statusLine = apacheHttpResponse.getStatusLine();
    final int statusCode;
    final String reasonPhrase;
    if (statusLine == null) {
        statusCode = -1;
        reasonPhrase = null;
    } else {
        statusCode = statusLine.getStatusCode();
        reasonPhrase = statusLine.getReasonPhrase();
    }
    CosHttpResponse response = createResponse(httpRequestBase, request, apacheHttpResponse);
    CosServiceException exception = null;
    try {
        exception = errorResponseHandler.handle(response);
        log.debug("Received error response: " + exception);
    } catch (Exception e) {
        // responses that don't have any content
        if (statusCode == 413) {
            exception = new CosServiceException("Request entity too large");
            exception.setStatusCode(statusCode);
            exception.setErrorType(ErrorType.Client);
            exception.setErrorCode("Request entity too large");
        } else if (statusCode == 503 && "Service Unavailable".equalsIgnoreCase(reasonPhrase)) {
            exception = new CosServiceException("Service unavailable");
            exception.setStatusCode(statusCode);
            exception.setErrorType(ErrorType.Service);
            exception.setErrorCode("Service unavailable");
        } else {
            String errorMessage = "Unable to unmarshall error response (" + e.getMessage() + "). Response Code: " + (statusLine == null ? "None" : statusCode) + ", Response Text: " + reasonPhrase;
            throw new CosClientException(errorMessage, e);
        }
    }
    exception.setStatusCode(statusCode);
    exception.fillInStackTrace();
    return exception;
}
Also used : StatusLine(org.apache.http.StatusLine) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) URISyntaxException(java.net.URISyntaxException) ResponseNotCompleteException(com.qcloud.cos.exception.ResponseNotCompleteException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException)

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