Search in sources :

Example 6 with GetObjectRequest

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

the class TransferManagerTest method testTransferManagerUploadDownBigFile.

@Test
public void testTransferManagerUploadDownBigFile() throws IOException, CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    TransferManager transferManager = new TransferManager(cosclient);
    File localFile = buildTestFile(1024 * 1024 * 10L);
    File downFile = new File(localFile.getAbsolutePath() + ".down");
    String key = "ut/" + localFile.getName();
    String destKey = key + ".copy";
    try {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setServerSideEncryption("AES256");
        putObjectRequest.setMetadata(objectMetadata);
        Upload upload = transferManager.upload(putObjectRequest);
        UploadResult uploadResult = upload.waitForUploadResult();
        assertTrue(uploadResult.getETag().contains("-"));
        assertTrue(upload.isResumeableMultipartUploadAfterFailed());
        assertNotNull(upload.getResumeableMultipartUploadId());
        assertNotNull(uploadResult.getRequestId());
        assertNotNull(uploadResult.getDateStr());
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        Download download = transferManager.download(getObjectRequest, downFile);
        download.waitForCompletion();
        // check file
        assertEquals(Md5Utils.md5Hex(localFile), Md5Utils.md5Hex(downFile));
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, key, bucket, destKey);
        Copy copy = transferManager.copy(copyObjectRequest, cosclient, null);
        copy.waitForCompletion();
    } finally {
        // delete file on cos
        clearObject(key);
        // delete file on cos
        clearObject(destKey);
        if (localFile.exists()) {
            assertTrue(localFile.delete());
        }
        if (downFile.exists()) {
            assertTrue(downFile.delete());
        }
    }
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) Copy(com.qcloud.cos.transfer.Copy) MultipleFileUpload(com.qcloud.cos.transfer.MultipleFileUpload) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) MultipleFileDownload(com.qcloud.cos.transfer.MultipleFileDownload) Download(com.qcloud.cos.transfer.Download) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Example 7 with GetObjectRequest

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

the class TransferManagerTest method testTransferManagerUploadDownCopySmallFile.

@Test
public void testTransferManagerUploadDownCopySmallFile() throws IOException, CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    TransferManager transferManager = new TransferManager(cosclient);
    File localFile = buildTestFile(1024 * 1024 * 2L);
    File downFile = new File(localFile.getAbsolutePath() + ".down");
    String key = "ut/" + localFile.getName();
    String destKey = key + ".copy";
    try {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
        Upload upload = transferManager.upload(putObjectRequest);
        UploadResult uploadResult = upload.waitForUploadResult();
        // head object
        headSimpleObject(key, localFile.length(), Md5Utils.md5Hex(localFile));
        assertEquals(Md5Utils.md5Hex(localFile), uploadResult.getETag());
        assertFalse(upload.isResumeableMultipartUploadAfterFailed());
        assertNull(upload.getResumeableMultipartUploadId());
        assertNotNull(uploadResult.getRequestId());
        assertNotNull(uploadResult.getDateStr());
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        Download download = transferManager.download(getObjectRequest, downFile);
        download.waitForCompletion();
        // check file
        assertEquals(Md5Utils.md5Hex(localFile), Md5Utils.md5Hex(downFile));
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, key, bucket, destKey);
        Copy copy = transferManager.copy(copyObjectRequest, cosclient, null);
        copy.waitForCompletion();
    } finally {
        // clear object
        clearObject(key);
        // clear dest object
        clearObject(destKey);
        // delete smallfile
        if (localFile.exists()) {
            assertTrue(localFile.delete());
        }
        if (downFile.exists()) {
            assertTrue(downFile.delete());
        }
    }
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) Copy(com.qcloud.cos.transfer.Copy) MultipleFileUpload(com.qcloud.cos.transfer.MultipleFileUpload) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) MultipleFileDownload(com.qcloud.cos.transfer.MultipleFileDownload) Download(com.qcloud.cos.transfer.Download) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Example 8 with GetObjectRequest

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

the class AbstractCOSClientTest method getObject.

// 下载COS的object
protected static void getObject(String key, File localDownFile, long[] range, long expectedLength, String expectedMd5) {
    System.setProperty(SkipMd5CheckStrategy.DISABLE_GET_OBJECT_MD5_VALIDATION_PROPERTY, "true");
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
    ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
    String responseContentType = "image/x-icon";
    String responseContentLanguage = "zh-CN";
    String responseContentDispositon = "filename=\"abc.txt\"";
    String responseCacheControl = "no-cache";
    String expireStr = DateUtils.formatRFC822Date(new Date(System.currentTimeMillis() + 24 * 3600 * 1000));
    responseHeaders.setContentType(responseContentType);
    responseHeaders.setContentLanguage(responseContentLanguage);
    responseHeaders.setContentDisposition(responseContentDispositon);
    responseHeaders.setCacheControl(responseCacheControl);
    responseHeaders.setExpires(expireStr);
    getObjectRequest.setResponseHeaders(responseHeaders);
    if (range != null) {
        if (range[1] == range[0] && range[1] == 0) {
            assertEquals(expectedLength, 1);
            getObjectRequest.setRange(range[0], range[1]);
        } else {
            assertEquals(expectedLength, range[1] - range[0] + 1);
            getObjectRequest.setRange(range[0], range[1]);
        }
    }
    try {
        ObjectMetadata objectMetadata = cosclient.getObject(getObjectRequest, localDownFile);
        assertEquals(responseContentType, objectMetadata.getContentType());
        assertEquals(responseContentLanguage, objectMetadata.getContentLanguage());
        assertEquals(responseContentDispositon, objectMetadata.getContentDisposition());
        assertEquals(responseCacheControl, objectMetadata.getCacheControl());
        assertEquals(expectedLength, localDownFile.length());
        assertEquals(expectedMd5, Md5Utils.md5Hex(localDownFile));
    } catch (SecurityException se) {
        if (cosclient instanceof COSEncryptionClient && cryptoConfiguration != null && cryptoConfiguration.getCryptoMode() == CryptoMode.StrictAuthenticatedEncryption && range != null) {
            return;
        }
        fail(se.toString());
    } catch (Exception e) {
        fail(e.toString());
    }
}
Also used : ResponseHeaderOverrides(com.qcloud.cos.model.ResponseHeaderOverrides) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) Date(java.util.Date) CosServiceException(com.qcloud.cos.exception.CosServiceException) IOException(java.io.IOException)

Example 9 with GetObjectRequest

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

the class ListVersionsTest method headAndGetVersion.

private void headAndGetVersion(String key, String versionId, String expectedEtag, long expectedLength, File downloadLocalFile) throws CosServiceException, FileNotFoundException, IOException {
    GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest(bucket, key, versionId);
    ObjectMetadata objectMetadata = cosclient.getObjectMetadata(getObjectMetadataRequest);
    assertFalse(objectMetadata.isDeleteMarker());
    assertEquals(expectedLength, objectMetadata.getContentLength());
    assertEquals(expectedEtag, objectMetadata.getETag());
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key, versionId);
    ObjectMetadata objectMetadata2 = cosclient.getObject(getObjectRequest, downloadLocalFile);
    assertEquals(expectedLength, downloadLocalFile.length());
    assertEquals(expectedEtag, Md5Utils.md5Hex(downloadLocalFile));
    assertFalse(objectMetadata2.isDeleteMarker());
    assertEquals(expectedLength, objectMetadata2.getContentLength());
    assertEquals(expectedEtag, objectMetadata2.getETag());
    assertTrue(downloadLocalFile.delete());
}
Also used : GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest)

Example 10 with GetObjectRequest

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

the class TransferManager method resumeDownload.

/**
 * Resumes an download operation. This download operation uses the same configuration as the
 * original download. Any data already fetched will be skipped, and only the remaining data is
 * retrieved from Qcloud COS.
 *
 * @param persistableDownload the download to resume.
 * @return A new <code>Download</code> object to use to check the state of the download, listen
 *         for progress notifications, and otherwise manage the download.
 *
 * @throws CosClientException If any errors are encountered in the client while making the
 *         request or handling the response.
 * @throws CosServiceException If any errors occurred in Qcloud COS while processing the
 *         request.
 */
public Download resumeDownload(PersistableDownload persistableDownload) {
    assertParameterNotNull(persistableDownload, "PausedDownload is mandatory to resume a download.");
    GetObjectRequest request = new GetObjectRequest(persistableDownload.getBucketName(), persistableDownload.getKey(), persistableDownload.getVersionId());
    if (persistableDownload.getRange() != null && persistableDownload.getRange().length == 2) {
        long[] range = persistableDownload.getRange();
        request.setRange(range[0], range[1]);
    }
    request.setResponseHeaders(persistableDownload.getResponseHeaders());
    return doDownload(request, new File(persistableDownload.getFile()), null, null, APPEND_MODE);
}
Also used : GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

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