Search in sources :

Example 81 with CosServiceException

use of com.qcloud.cos.exception.CosServiceException in project Tbed by Hello-hao.

the class COSImageupload method ImageuploadCOS.

public ReturnImage ImageuploadCOS(Map<String, File> fileMap, String username, Integer keyID) {
    ReturnImage returnImage = new ReturnImage();
    File file = null;
    Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
    try {
        for (Map.Entry<String, File> entry : fileMap.entrySet()) {
            String ShortUID = SetText.getShortUuid();
            file = entry.getValue();
            try {
                String bucketName = key.getBucketname();
                String userkey = username + "/" + ShortUID + "." + entry.getKey();
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, userkey, file);
                PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
                returnImage.setImgname(userkey);
                returnImage.setImgurl(key.getRequestAddress() + "/" + userkey);
                returnImage.setImgSize(entry.getValue().length());
                returnImage.setCode("200");
            } catch (CosServiceException serverException) {
                returnImage.setCode("400");
                serverException.printStackTrace();
            } catch (CosClientException clientException) {
                returnImage.setCode("400");
                clientException.printStackTrace();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        returnImage.setCode("500");
    }
    return returnImage;
}
Also used : CosClientException(com.qcloud.cos.exception.CosClientException) CosClientException(com.qcloud.cos.exception.CosClientException) CosServiceException(com.qcloud.cos.exception.CosServiceException) ReturnImage(cn.hellohao.pojo.ReturnImage) CosServiceException(com.qcloud.cos.exception.CosServiceException) File(java.io.File)

Example 82 with CosServiceException

use of com.qcloud.cos.exception.CosServiceException in project alluxio by Alluxio.

the class COSInputStream method createStream.

@Override
protected InputStream createStream(long startPos, long endPos) throws IOException {
    GetObjectRequest req = new GetObjectRequest(mBucketName, mKey);
    // COS returns entire object if we read past the end
    req.setRange(startPos, endPos < mContentLength ? endPos - 1 : mContentLength - 1);
    CosServiceException lastException = null;
    String errorMessage = String.format("Failed to open key: %s bucket: %s", mKey, mBucketName);
    while (mRetryPolicy.attempt()) {
        try {
            COSObject object = mCosClient.getObject(req);
            return new BufferedInputStream(object.getObjectContent());
        } catch (CosServiceException e) {
            errorMessage = String.format("Failed to open key: %s bucket: %s attempts: %d error: %s", mKey, mBucketName, mRetryPolicy.getAttemptCount(), e.getMessage());
            if (e.getStatusCode() != HttpStatus.SC_NOT_FOUND) {
                throw new IOException(errorMessage, e);
            }
            // Key does not exist
            lastException = e;
        }
    }
    // Failed after retrying key does not exist
    throw new IOException(errorMessage, lastException);
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) BufferedInputStream(java.io.BufferedInputStream) COSObject(com.qcloud.cos.model.COSObject) IOException(java.io.IOException) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest)

Aggregations

CosServiceException (com.qcloud.cos.exception.CosServiceException)82 CosClientException (com.qcloud.cos.exception.CosClientException)64 COSClient (com.qcloud.cos.COSClient)37 ClientConfig (com.qcloud.cos.ClientConfig)37 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)37 COSCredentials (com.qcloud.cos.auth.COSCredentials)37 Region (com.qcloud.cos.region.Region)37 File (java.io.File)28 IOException (java.io.IOException)20 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)15 TransferManager (com.qcloud.cos.transfer.TransferManager)14 ExecutorService (java.util.concurrent.ExecutorService)14 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)13 ByteArrayInputStream (java.io.ByteArrayInputStream)13 PutObjectResult (com.qcloud.cos.model.PutObjectResult)12 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)11 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)11 ArrayList (java.util.ArrayList)10 LinkedList (java.util.LinkedList)10 Test (org.junit.Test)10