Search in sources :

Example 1 with Tag

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

the class ObjectTaggingDemo method SetGetDelObjectTaggingDemo.

public static void SetGetDelObjectTaggingDemo() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("COS_SECRET_ID", "COS_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);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    cosclient.putObject(bucketName, key, "data");
    List<Tag> tags = new LinkedList<>();
    tags.add(new Tag("key", "value"));
    tags.add(new Tag("key-1", "value-1"));
    ObjectTagging objectTagging = new ObjectTagging(tags);
    SetObjectTaggingRequest setObjectTaggingRequest = new SetObjectTaggingRequest(bucketName, key, objectTagging);
    cosclient.setObjectTagging(setObjectTaggingRequest);
    GetObjectTaggingResult getObjectTaggingResult = cosclient.getObjectTagging(new GetObjectTaggingRequest(bucketName, key));
    List<Tag> resultTagSet = getObjectTaggingResult.getTagSet();
    cosclient.deleteObjectTagging(new DeleteObjectTaggingRequest(bucketName, key));
    GetObjectTaggingResult getObjectTaggingResultSecond = cosclient.getObjectTagging(new GetObjectTaggingRequest(bucketName, key));
    List<Tag> resultTagSetSecond = getObjectTaggingResultSecond.getTagSet();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) LinkedList(java.util.LinkedList) COSClient(com.qcloud.cos.COSClient) Region(com.qcloud.cos.region.Region) Tag(com.qcloud.cos.model.Tag.Tag) ClientConfig(com.qcloud.cos.ClientConfig)

Example 2 with Tag

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

the class ObjectTaggingXmlFactory method convertToXmlByteArray.

public byte[] convertToXmlByteArray(ObjectTagging tagging) {
    XmlWriter writer = new XmlWriter();
    writer.start("Tagging").start("TagSet");
    for (Tag tag : tagging.getTagSet()) {
        writer.start("Tag");
        writer.start("Key").value(tag.getKey()).end();
        writer.start("Value").value(tag.getValue()).end();
        // </Tag>
        writer.end();
    }
    // </TagSet>
    writer.end();
    // </Tagging>
    writer.end();
    return writer.getBytes();
}
Also used : Tag(com.qcloud.cos.model.Tag.Tag) XmlWriter(com.qcloud.cos.internal.XmlWriter)

Example 3 with Tag

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

the class ObjecTaggingTest method testSetGetDelObjectTagging.

@Test
public void testSetGetDelObjectTagging() throws IOException {
    String key = "tagging.txt";
    try {
        cosclient.putObject(bucket, key, "data");
        List<Tag> tags = new LinkedList<>();
        tags.add(new Tag("key", "value"));
        tags.add(new Tag("key-1", "value-1"));
        ObjectTagging objectTagging = new ObjectTagging(tags);
        SetObjectTaggingRequest setObjectTaggingRequest = new SetObjectTaggingRequest(bucket, key, objectTagging);
        cosclient.setObjectTagging(setObjectTaggingRequest);
        GetObjectTaggingResult getObjectTaggingResult = cosclient.getObjectTagging(new GetObjectTaggingRequest(bucket, key));
        assertEquals(getObjectTaggingResult.getTagSet(), tags);
        ;
        cosclient.deleteObjectTagging(new DeleteObjectTaggingRequest(bucket, key));
        GetObjectTaggingResult getObjectTaggingResultSecond = cosclient.getObjectTagging(new GetObjectTaggingRequest(bucket, key));
        List<Tag> resultTagSetSecond = getObjectTaggingResultSecond.getTagSet();
        assertEquals(resultTagSetSecond.size(), 0);
    } finally {
        cosclient.deleteObject(bucket, key);
    }
}
Also used : Tag(com.qcloud.cos.model.Tag.Tag) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Tag (com.qcloud.cos.model.Tag.Tag)3 LinkedList (java.util.LinkedList)2 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 XmlWriter (com.qcloud.cos.internal.XmlWriter)1 Region (com.qcloud.cos.region.Region)1 Test (org.junit.Test)1