Search in sources :

Example 6 with ObjectMetadata

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

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

the class BucketTaggingDemo method SetTagWhilePutObject.

public static void SetTagWhilePutObject() {
    // 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-shanghai"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "testTag";
    InputStream is = new ByteArrayInputStream(new byte[] { 'd', 'a', 't', 'a' });
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setHeader("x-cos-tagging", "level=info");
    cosclient.putObject(bucketName, key, is, objectMetadata);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 8 with ObjectMetadata

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

the class PicOperationDemo method PicPersistentProcessing.

public static void PicPersistentProcessing() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("SECRET_ID", "SECRET_KEY");
    // 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);
    // 4. bucket名需包含appid
    String bucketName = "examplebucket-1250000000";
    String key = "example.jpg";
    File localFile = new File("/data/example.jpg");
    // 5.对图像进行持久化处理
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setHeader("Pic-Operations", "{\"is_pic_info\":1,\"rules\":[{\"fileid\":\"example.png\",\"rule\":\"imageView2/format/png\"}]}");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    putObjectRequest.setMetadata(objectMetadata);
    try {
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        // putobjectResult会返回文件的etag
        String etag = putObjectResult.getETag();
    } 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) PutObjectResult(com.qcloud.cos.model.PutObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 9 with ObjectMetadata

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

the class PutObjectCopyTest method testUpdateDiffSize.

private void testUpdateDiffSize(long fileSize, ObjectMetadata newObjectMetaData) throws IOException {
    if (!judgeUserInfoValid()) {
        return;
    }
    File localFile = buildTestFile(fileSize);
    String srcEtag = Md5Utils.md5Hex(localFile);
    String srcKey = String.format("ut/src_len_%d.txt", fileSize);
    try {
        putObjectFromLocalFile(localFile, srcKey);
        cosclient.updateObjectMetaData(bucket, srcKey, newObjectMetaData);
        ObjectMetadata destObjectMetadata = headSimpleObject(srcKey, fileSize, srcEtag);
        if (newObjectMetaData != null) {
            checkMetaData(newObjectMetaData, destObjectMetadata);
        }
    } finally {
        // delete file on cos
        clearObject(srcKey);
        // delete local file
        if (localFile.exists()) {
            assertTrue(localFile.delete());
        }
    }
}
Also used : File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 10 with ObjectMetadata

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

the class PutObjectCopyTest method testCopySameRegionEmptyWithNewMetaData.

@Test
public void testCopySameRegionEmptyWithNewMetaData() throws IOException {
    ObjectMetadata newObjectMetadata = new ObjectMetadata();
    newObjectMetadata.setServerSideEncryption("AES256");
    newObjectMetadata.setContentType("image/tiff");
    testCopySameRegionDiffSize(0, newObjectMetadata);
}
Also used : ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) Test(org.junit.Test)

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