Search in sources :

Example 1 with COSObject

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

the class PutObjectDemo method putObjectDemo.

static void putObjectDemo() {
    String bucketName = "examplebucket-1251668577";
    String key = "abc/abc.txt";
    String localPath = "abc.txt";
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setHeader("expires", new Date(1660000000000L));
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(localPath));
    putObjectRequest.withMetadata(objectMetadata);
    PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
    System.out.println(putObjectResult.getRequestId());
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    COSObject cosObject = cosClient.getObject(getObjectRequest);
    System.out.println(cosObject.getObjectMetadata().getRequestId());
    cosClient.shutdown();
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) COSObject(com.qcloud.cos.model.COSObject) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Date(java.util.Date) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 2 with COSObject

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

the class PutGetDelTest method testGetObjectIfMatchWrongEtag.

@Test
public void testGetObjectIfMatchWrongEtag() 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);
        // 打乱一下,得到一个错误的etag
        String wrongEtag = fileEtag.substring(5) + fileEtag.substring(0, 5);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        List<String> eTagList = new ArrayList<>();
        eTagList.add(wrongEtag);
        getObjectRequest.setMatchingETagConstraints(eTagList);
        COSObject cosObject = cosclient.getObject(getObjectRequest);
        assertNull(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 3 with COSObject

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

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

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

the class DownloadCallable method call.

/**
 * This method must return a non-null object, or else the existing implementation in
 * {@link AbstractTransfer#waitForCompletion()} would block forever.
 *
 * @return the downloaded file
 */
@Override
public File call() throws Exception {
    try {
        latch.await();
        download.setState(TransferState.InProgress);
        COSObject cosObject = retryableDownloadCOSObjectToFile(dstfile, new DownloadTaskImpl(cos, download, req), resumeExistingDownload);
        if (cosObject == null) {
            download.setState(TransferState.Canceled);
            download.setMonitor(new DownloadMonitor(download, null));
        } else {
            download.setState(TransferState.Completed);
        }
        return dstfile;
    } catch (Throwable t) {
        // Downloads aren't allowed to move from canceled to failed
        if (download.getState() != TransferState.Canceled) {
            download.setState(TransferState.Failed);
        }
        if (t instanceof Exception)
            throw (Exception) t;
        else
            throw (Error) t;
    }
}
Also used : COSObject(com.qcloud.cos.model.COSObject) FileLockException(com.qcloud.cos.exception.FileLockException) SSLProtocolException(javax.net.ssl.SSLProtocolException) CosClientException(com.qcloud.cos.exception.CosClientException) SocketException(java.net.SocketException)

Aggregations

COSObject (com.qcloud.cos.model.COSObject)21 CosServiceException (com.qcloud.cos.exception.CosServiceException)11 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)11 CosClientException (com.qcloud.cos.exception.CosClientException)8 IOException (java.io.IOException)7 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)6 File (java.io.File)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)4 ResponseNotCompleteException (com.qcloud.cos.exception.ResponseNotCompleteException)3 SocketException (java.net.SocketException)3 SSLProtocolException (javax.net.ssl.SSLProtocolException)3 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)2 JeesuiteBaseException (com.jeesuite.common.JeesuiteBaseException)1 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)1 FileLockException (com.qcloud.cos.exception.FileLockException)1 AbstractPutObjectRequest (com.qcloud.cos.model.AbstractPutObjectRequest)1 COSObjectId (com.qcloud.cos.model.COSObjectId)1 COSObjectInputStream (com.qcloud.cos.model.COSObjectInputStream)1 EncryptedGetObjectRequest (com.qcloud.cos.model.EncryptedGetObjectRequest)1