Search in sources :

Example 1 with AbortMultipartUploadRequest

use of com.qcloud.cos.model.AbortMultipartUploadRequest in project cos-java-sdk-v5 by tencentyun.

the class MultipartUploadTest method testAbortMultipartUploads.

@Test
public void testAbortMultipartUploads() {
    if (!judgeUserInfoValid()) {
        return;
    }
    String key = "ut/testAbortMultipart.txt";
    String uploadId = testInitMultipart(key);
    AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucket, key, uploadId);
    cosclient.abortMultipartUpload(abortMultipartUploadRequest);
    try {
        testListMultipart(key, uploadId, 1, new ArrayList<String>());
        fail("Aborted uploadid is still valid. uploadId:" + uploadId);
    } catch (CosServiceException e) {
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) AbortMultipartUploadRequest(com.qcloud.cos.model.AbortMultipartUploadRequest) Test(org.junit.Test)

Example 2 with AbortMultipartUploadRequest

use of com.qcloud.cos.model.AbortMultipartUploadRequest in project cos-java-sdk-v5 by tencentyun.

the class CopyCallable method abortMultipartCopy.

private void abortMultipartCopy() {
    try {
        AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest(copyObjectRequest.getDestinationBucketName(), copyObjectRequest.getDestinationKey(), multipartUploadId);
        TransferManagerUtils.populateEndpointAddr(copyObjectRequest, abortRequest);
        cos.abortMultipartUpload(abortRequest);
    } catch (Exception e) {
        log.info("Unable to abort multipart upload, you may need to manually remove uploaded parts: " + e.getMessage(), e);
    }
}
Also used : AbortMultipartUploadRequest(com.qcloud.cos.model.AbortMultipartUploadRequest) CancellationException(java.util.concurrent.CancellationException)

Example 3 with AbortMultipartUploadRequest

use of com.qcloud.cos.model.AbortMultipartUploadRequest in project hadoop-cos by tencentyun.

the class CosNativeFileSystemStore method abortMultipartUpload.

public void abortMultipartUpload(String key, String uploadId) throws IOException {
    LOG.info("Ready to abort the multipart upload. cos key: {}, upload id: {}.", key, uploadId);
    try {
        AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, uploadId);
        this.callCOSClientWithRetry(abortMultipartUploadRequest);
    } catch (Exception e) {
        String errMsg = String.format("Aborting the multipart upload failed. cos key: %s, upload id: %s. " + "exception: %s.", key, uploadId, e);
        handleException(new Exception(errMsg), key);
    }
}
Also used : AbortMultipartUploadRequest(com.qcloud.cos.model.AbortMultipartUploadRequest) ResponseNotCompleteException(com.qcloud.cos.exception.ResponseNotCompleteException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException)

Example 4 with AbortMultipartUploadRequest

use of com.qcloud.cos.model.AbortMultipartUploadRequest in project hadoop-cos by tencentyun.

the class CosNativeFileSystemStore method callCOSClientWithRetry.

// posix bucket mkdir if the middle part exist will return the 500 error,
// and the rename if the dst exist will return the 500 status too,
// which make the related 5** retry useless. mo improve the resp info to filter.
private <X> Object callCOSClientWithRetry(X request) throws CosServiceException, IOException {
    String sdkMethod = "";
    int retryIndex = 1;
    int l5ErrorCodeRetryIndex = 1;
    while (true) {
        try {
            if (request instanceof PutObjectRequest) {
                sdkMethod = "putObject";
                if (((PutObjectRequest) request).getInputStream().markSupported()) {
                    ((PutObjectRequest) request).getInputStream().mark((int) ((PutObjectRequest) request).getMetadata().getContentLength());
                }
                return this.cosClient.putObject((PutObjectRequest) request);
            } else if (request instanceof UploadPartRequest) {
                sdkMethod = "uploadPart";
                if (((UploadPartRequest) request).getInputStream().markSupported()) {
                    ((UploadPartRequest) request).getInputStream().mark((int) ((UploadPartRequest) request).getPartSize());
                }
                return this.cosClient.uploadPart((UploadPartRequest) request);
            } else if (request instanceof CopyPartRequest) {
                sdkMethod = "copyPartRequest";
                return this.cosClient.copyPart((CopyPartRequest) request);
            } else if (request instanceof HeadBucketRequest) {
                // use for checking bucket type
                sdkMethod = "headBucket";
                return this.cosClient.headBucket((HeadBucketRequest) request);
            } else if (request instanceof RenameRequest) {
                sdkMethod = "rename";
                this.cosClient.rename((RenameRequest) request);
                return new Object();
            } else if (request instanceof GetObjectMetadataRequest) {
                sdkMethod = "queryObjectMeta";
                return this.cosClient.getObjectMetadata((GetObjectMetadataRequest) request);
            } else if (request instanceof DeleteObjectRequest) {
                sdkMethod = "deleteObject";
                this.cosClient.deleteObject((DeleteObjectRequest) request);
                return new Object();
            } else if (request instanceof CopyObjectRequest) {
                sdkMethod = "copyFile";
                return this.cosClient.copyObject((CopyObjectRequest) request);
            } else if (request instanceof GetObjectRequest) {
                sdkMethod = "getObject";
                return this.cosClient.getObject((GetObjectRequest) request);
            } else if (request instanceof ListObjectsRequest) {
                sdkMethod = "listObjects";
                return this.cosClient.listObjects((ListObjectsRequest) request);
            } else if (request instanceof InitiateMultipartUploadRequest) {
                sdkMethod = "initiateMultipartUpload";
                return this.cosClient.initiateMultipartUpload((InitiateMultipartUploadRequest) request);
            } else if (request instanceof CompleteMultipartUploadRequest) {
                sdkMethod = "completeMultipartUpload";
                return this.cosClient.completeMultipartUpload((CompleteMultipartUploadRequest) request);
            } else if (request instanceof AbortMultipartUploadRequest) {
                sdkMethod = "abortMultipartUpload";
                this.cosClient.abortMultipartUpload((AbortMultipartUploadRequest) request);
                return new Object();
            } else {
                throw new IOException("no such method");
            }
        } catch (ResponseNotCompleteException nce) {
            if (this.completeMPUCheckEnabled && request instanceof CompleteMultipartUploadRequest) {
                String key = ((CompleteMultipartUploadRequest) request).getKey();
                FileMetadata fileMetadata = this.queryObjectMetadata(key);
                if (null == fileMetadata) {
                    // if file not exist must throw the exception.
                    handleException(nce, key);
                }
                LOG.warn("Complete mpu resp not complete key [{}]", key);
                // todo: some other double check after cgi unified the ctime of mpu.
                return new CompleteMultipartUploadResult();
            } else {
                throw new IOException(nce);
            }
        } catch (CosServiceException cse) {
            String errMsg = String.format("all cos sdk failed, retryIndex: [%d / %d], " + "call method: %s, exception: %s", retryIndex, this.maxRetryTimes, sdkMethod, cse);
            int statusCode = cse.getStatusCode();
            String errorCode = cse.getErrorCode();
            LOG.debug("fail to retry statusCode {}, errorCode {}", statusCode, errorCode);
            // 对5xx错误进行重试
            if (request instanceof CopyObjectRequest && hasErrorCode(statusCode, errorCode)) {
                if (retryIndex <= this.maxRetryTimes) {
                    LOG.info(errMsg, cse);
                    ++retryIndex;
                } else {
                    LOG.error(errMsg, cse);
                    throw new IOException(errMsg);
                }
            } else if (request instanceof CompleteMultipartUploadRequest && hasErrorCode(statusCode, errorCode)) {
                // complete mpu error code might be in body when status code is 200
                // double check to head object only works in big data job case which key is not same.
                String key = ((CompleteMultipartUploadRequest) request).getKey();
                FileMetadata fileMetadata = this.queryObjectMetadata(key);
                if (null != fileMetadata) {
                    // if file exist direct return.
                    LOG.info("complete mpu error in body, error code {}, but key {} already exist, length {}", errorCode, key, fileMetadata.getLength());
                    return new CompleteMultipartUploadResult();
                }
                // here same like the copy request not setting the interval sleep for now
                if (retryIndex <= this.maxRetryTimes) {
                    LOG.info(errMsg, cse);
                    ++retryIndex;
                } else {
                    LOG.error(errMsg, cse);
                    throw new IOException(errMsg);
                }
            } else if (statusCode / 100 == 5) {
                if (retryIndex <= this.maxRetryTimes) {
                    if (statusCode == 503) {
                        if (useL5Id) {
                            if (l5ErrorCodeRetryIndex >= this.l5UpdateMaxRetryTimes) {
                                // L5上报,进行重试
                                l5EndpointResolver.updateRouteResult(-1);
                                l5ErrorCodeRetryIndex = 1;
                            } else {
                                l5ErrorCodeRetryIndex = l5ErrorCodeRetryIndex + 1;
                            }
                        }
                    }
                    LOG.info(errMsg, cse);
                    long sleepLeast = retryIndex * 300L;
                    long sleepBound = retryIndex * 500L;
                    try {
                        if (request instanceof PutObjectRequest) {
                            LOG.info("Try to reset the put object request input stream.");
                            if (((PutObjectRequest) request).getInputStream().markSupported()) {
                                ((PutObjectRequest) request).getInputStream().reset();
                            } else {
                                LOG.error("The put object request input stream can not be reset, so it can not be" + " retried.");
                                throw cse;
                            }
                        }
                        if (request instanceof UploadPartRequest) {
                            LOG.info("Try to reset the upload part request input stream.");
                            if (((UploadPartRequest) request).getInputStream().markSupported()) {
                                ((UploadPartRequest) request).getInputStream().reset();
                            } else {
                                LOG.error("The upload part request input stream can not be reset, so it can not " + "be retried" + ".");
                                throw cse;
                            }
                        }
                        // if direct retry may occur 403 not found the upload id.
                        if (request instanceof CompleteMultipartUploadRequest && statusCode == 503) {
                            String key = ((CompleteMultipartUploadRequest) request).getKey();
                            FileMetadata fileMetadata = this.queryObjectMetadata(key);
                            if (null != fileMetadata) {
                                // if file exist direct return.
                                LOG.info("complete mpu error might access time out, " + "but key {} already exist, length {}", key, fileMetadata.getLength());
                                return new CompleteMultipartUploadResult();
                            }
                        }
                        Thread.sleep(ThreadLocalRandom.current().nextLong(sleepLeast, sleepBound));
                        ++retryIndex;
                    } catch (InterruptedException e) {
                        throw new IOException(e.toString());
                    }
                } else {
                    LOG.error(errMsg, cse);
                    throw new IOException(errMsg);
                }
            } else {
                throw cse;
            }
        } catch (Exception e) {
            throw new IOException(e);
        }
    }
}
Also used : HeadBucketRequest(com.qcloud.cos.model.HeadBucketRequest) ResponseNotCompleteException(com.qcloud.cos.exception.ResponseNotCompleteException) InitiateMultipartUploadRequest(com.qcloud.cos.model.InitiateMultipartUploadRequest) UploadPartRequest(com.qcloud.cos.model.UploadPartRequest) AbortMultipartUploadRequest(com.qcloud.cos.model.AbortMultipartUploadRequest) IOException(java.io.IOException) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) ResponseNotCompleteException(com.qcloud.cos.exception.ResponseNotCompleteException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException) DeleteObjectRequest(com.qcloud.cos.model.DeleteObjectRequest) CopyPartRequest(com.qcloud.cos.model.CopyPartRequest) ListObjectsRequest(com.qcloud.cos.model.ListObjectsRequest) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) RenameRequest(com.qcloud.cos.model.RenameRequest) COSObject(com.qcloud.cos.model.COSObject) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) CompleteMultipartUploadRequest(com.qcloud.cos.model.CompleteMultipartUploadRequest)

Example 5 with AbortMultipartUploadRequest

use of com.qcloud.cos.model.AbortMultipartUploadRequest 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)

Aggregations

AbortMultipartUploadRequest (com.qcloud.cos.model.AbortMultipartUploadRequest)8 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 CosClientException (com.qcloud.cos.exception.CosClientException)3 ResponseNotCompleteException (com.qcloud.cos.exception.ResponseNotCompleteException)2 ListMultipartUploadsRequest (com.qcloud.cos.model.ListMultipartUploadsRequest)2 MultipartUpload (com.qcloud.cos.model.MultipartUpload)2 MultipartUploadListing (com.qcloud.cos.model.MultipartUploadListing)2 IOException (java.io.IOException)2 CancellationException (java.util.concurrent.CancellationException)2 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 COSObject (com.qcloud.cos.model.COSObject)1 CompleteMultipartUploadRequest (com.qcloud.cos.model.CompleteMultipartUploadRequest)1 CompleteMultipartUploadResult (com.qcloud.cos.model.CompleteMultipartUploadResult)1 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)1 CopyPartRequest (com.qcloud.cos.model.CopyPartRequest)1 DeleteObjectRequest (com.qcloud.cos.model.DeleteObjectRequest)1 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)1