Search in sources :

Example 11 with GetObjectRequest

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

the class PutGetDelTest method testGetObjectIfNoneMatchWrongEtag.

@Test
public void testGetObjectIfNoneMatchWrongEtag() throws IOException {
    if (!judgeUserInfoValid()) {
        return;
    }
    File localFile = buildTestFile(1024);
    String key = "ut/" + localFile.getName();
    cosclient.putObject(bucket, key, localFile);
    try {
        String fileEtag = Md5Utils.md5Hex(localFile);
        String wrongEtag = fileEtag.substring(5) + fileEtag.substring(0, 5);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        List<String> eTagList = new ArrayList<>();
        eTagList.add("\"" + wrongEtag + "\"");
        getObjectRequest.setNonmatchingETagConstraints(eTagList);
        COSObject cosObject = cosclient.getObject(getObjectRequest);
        assertNotNull(cosObject);
    } catch (CosServiceException cse) {
        fail(cse.toString());
    } finally {
        cosclient.deleteObject(bucket, key);
        assertTrue(localFile.delete());
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) COSObject(com.qcloud.cos.model.COSObject) ArrayList(java.util.ArrayList) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Test(org.junit.Test)

Example 12 with GetObjectRequest

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

the class PutGetDelTest method testGetObjectIfMatchRightEtag.

@Test
public void testGetObjectIfMatchRightEtag() throws IOException {
    if (!judgeUserInfoValid()) {
        return;
    }
    File localFile = buildTestFile(1024);
    String key = "ut/" + localFile.getName();
    cosclient.putObject(bucket, key, localFile);
    try {
        String fileEtag = Md5Utils.md5Hex(localFile);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        List<String> eTagList = new ArrayList<>();
        eTagList.add("\"" + fileEtag + "\"");
        getObjectRequest.setMatchingETagConstraints(eTagList);
        COSObject cosObject = cosclient.getObject(getObjectRequest);
        assertNotNull(cosObject);
    } catch (CosServiceException cse) {
        fail(cse.toString());
    } finally {
        cosclient.deleteObject(bucket, key);
        assertTrue(localFile.delete());
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) COSObject(com.qcloud.cos.model.COSObject) ArrayList(java.util.ArrayList) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Test(org.junit.Test)

Example 13 with GetObjectRequest

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

the class ResumableDownloadSubmitter method submit.

public void submit() throws Exception {
    long contentLength = Long.parseLong(downloadRecord.getContentLength());
    download.setState(TransferState.InProgress);
    if (contentLength < this.multiThreadThreshold) {
        throw new CosClientException("contentLenth " + contentLength + " < " + this.multiThreadThreshold + " should not use resumabledownload.");
    }
    long start = 0;
    publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
    while (contentLength > start) {
        long bytesToRead = Math.min(partSize, contentLength - start);
        long end = start + bytesToRead - 1;
        String block = String.format("%d-%d", start, end);
        if (downloadRecord.hasDownloadedBlocks(block)) {
            log.debug("part found in download record: " + block);
            CRC64 crc64 = new CRC64();
            byte[] buffer = new byte[1024 * 10];
            int readBytes;
            destRandomAccessFile.seek(start);
            while (bytesToRead > 0 && (readBytes = destRandomAccessFile.read(buffer)) != -1) {
                long updateBytes = Math.min(readBytes, bytesToRead);
                bytesToRead -= updateBytes;
                crc64.update(buffer, (int) updateBytes);
            }
            skippedParts.add(new DownloadPart(start, end, crc64.getValue()));
            transferProgress.updateProgress(end + 1 - start);
        } else {
            GetObjectRequest getObj = copyGetObjectRequest(req);
            getObj.setRange(start, end);
            if (threadPool.isShutdown()) {
                publishProgress(listener, ProgressEventType.TRANSFER_CANCELED_EVENT);
                throw new CancellationException("TransferManager has been shutdown");
            }
            futures.add(threadPool.submit(new RangeDownloadCallable(cos, getObj, destFile, destFileChannel, downloadRecord)));
        }
        start = end + 1;
    }
}
Also used : CRC64(com.qcloud.cos.utils.CRC64) CancellationException(java.util.concurrent.CancellationException) CosClientException(com.qcloud.cos.exception.CosClientException) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest)

Example 14 with GetObjectRequest

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

the class ResumableDownloadSubmitter method copyGetObjectRequest.

GetObjectRequest copyGetObjectRequest(GetObjectRequest origin) {
    GetObjectRequest dst = new GetObjectRequest(origin.getCOSObjectId());
    dst.setCosCredentials(origin.getCosCredentials());
    dst.setFixedEndpointAddr(origin.getFixedEndpointAddr());
    dst.setGeneralProgressListener(origin.getGeneralProgressListener());
    dst.setMatchingETagConstraints(origin.getMatchingETagConstraints());
    dst.setModifiedSinceConstraint(origin.getModifiedSinceConstraint());
    dst.setNonmatchingETagConstraints(origin.getNonmatchingETagConstraints());
    dst.setResponseHeaders(origin.getResponseHeaders());
    dst.setSSECustomerKey(origin.getSSECustomerKey());
    dst.setTrafficLimit(origin.getTrafficLimit());
    dst.setUnmodifiedSinceConstraint(origin.getUnmodifiedSinceConstraint());
    return dst;
}
Also used : GetObjectRequest(com.qcloud.cos.model.GetObjectRequest)

Example 15 with GetObjectRequest

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

the class SymmetricKeyEncryptionClientDemo method getObjectDemo.

static void getObjectDemo() {
    // 下载文件
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    File downloadFile = new File("downSym.txt");
    ObjectMetadata objectMetadata = cosClient.getObject(getObjectRequest, downloadFile);
    System.out.println(objectMetadata.getRequestId());
}
Also used : GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Aggregations

GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)36 File (java.io.File)26 CosServiceException (com.qcloud.cos.exception.CosServiceException)16 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)14 COSObject (com.qcloud.cos.model.COSObject)12 CosClientException (com.qcloud.cos.exception.CosClientException)10 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)10 Test (org.junit.Test)9 TransferManager (com.qcloud.cos.transfer.TransferManager)8 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)7 COSCredentials (com.qcloud.cos.auth.COSCredentials)7 Download (com.qcloud.cos.transfer.Download)7 COSClient (com.qcloud.cos.COSClient)6 ClientConfig (com.qcloud.cos.ClientConfig)6 Region (com.qcloud.cos.region.Region)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)5 MultipleFileDownload (com.qcloud.cos.transfer.MultipleFileDownload)5 ExecutorService (java.util.concurrent.ExecutorService)5