Search in sources :

Example 11 with ObjectMetadata

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

the class PutObjectCopyTest method testUpdateObjectAttr.

@Test
public void testUpdateObjectAttr() throws IOException {
    ObjectMetadata newMetadata = new ObjectMetadata();
    newMetadata.setContentType("application/json");
    newMetadata.setContentDisposition("filename=\"abc.txt\"");
    newMetadata.setCacheControl("no-cache");
    newMetadata.setContentEncoding("gzip");
    newMetadata.addUserMetadata("school", "football sport");
    testUpdateDiffSize(0, newMetadata);
}
Also used : ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) Test(org.junit.Test)

Example 12 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata 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 13 with ObjectMetadata

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

the class AbstractCOSClientTest method testAppendGetDelObjectDiffSize.

protected void testAppendGetDelObjectDiffSize(long size, boolean isStream) throws IOException {
    String key = "ut/" + size;
    long nextAppendPosition = 0;
    for (int i = 0; i < 3; i++) {
        File localFile = buildTestFile(size);
        AppendObjectRequest appendObjectRequest = null;
        if (!isStream) {
            appendObjectRequest = new AppendObjectRequest(bucket, key, localFile);
        } else {
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(size);
            appendObjectRequest = new AppendObjectRequest(bucket, key, new FileInputStream(localFile), objectMetadata);
        }
        appendObjectRequest.setPosition(nextAppendPosition);
        AppendObjectResult appendObjectResult = cosclient.appendObject(appendObjectRequest);
        nextAppendPosition = appendObjectResult.getNextAppendPosition();
        localFile.delete();
    }
    ObjectMetadata objectMetadata = cosclient.getObjectMetadata(bucket, key);
    assertEquals(objectMetadata.getContentLength(), size * 3);
    cosclient.deleteObject(bucket, key);
}
Also used : AppendObjectRequest(com.qcloud.cos.model.AppendObjectRequest) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) FileInputStream(java.io.FileInputStream) AppendObjectResult(com.qcloud.cos.model.AppendObjectResult)

Example 14 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata 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 15 with ObjectMetadata

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

the class AbstractCOSClientTest method headSimpleObject.

protected static ObjectMetadata headSimpleObject(String key, long expectedLength, String expectedEtag) {
    ObjectMetadata objectMetadata = cosclient.getObjectMetadata(new GetObjectMetadataRequest(bucket, key));
    if (!useClientEncryption) {
        assertEquals(expectedLength, objectMetadata.getContentLength());
    } else {
        assertEquals(expectedLength, Long.valueOf(objectMetadata.getUserMetaDataOf(Headers.UNENCRYPTED_CONTENT_LENGTH)).longValue());
    }
    if (useClientEncryption) {
        assertEquals(false, expectedEtag.equals(objectMetadata.getETag()));
    } else {
        assertEquals(true, expectedEtag.equals(objectMetadata.getETag()));
    }
    assertNotNull(objectMetadata.getLastModified());
    return objectMetadata;
}
Also used : GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Aggregations

ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)77 CosClientException (com.qcloud.cos.exception.CosClientException)26 CosServiceException (com.qcloud.cos.exception.CosServiceException)23 IOException (java.io.IOException)22 File (java.io.File)21 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)17 COSClient (com.qcloud.cos.COSClient)15 ClientConfig (com.qcloud.cos.ClientConfig)14 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)14 PutObjectResult (com.qcloud.cos.model.PutObjectResult)14 Region (com.qcloud.cos.region.Region)14 ResponseNotCompleteException (com.qcloud.cos.exception.ResponseNotCompleteException)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 Test (org.junit.Test)11 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InputStream (java.io.InputStream)8 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)7 COSObject (com.qcloud.cos.model.COSObject)6