对象标签管理

更新时间: 2020-03-24

目录

设置对象标签

获取对象标签

删除对象标签

本节介绍如何管理对象标签。

对象标签使用一组键值对(Key-Value)来标记对象。

设置对象标签

以下代码用于设置对象标签:

// Endpoint以华北三为例,其它Region请按实际情况填写。
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";
String bucketName = "<yourBucketName>";
String key = "<yourObjectName>";

//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);

//设置对象标签
Tag tag1 = new Tag("key1", "value1");
Tag tag2 = new Tag("key2", "value2");
List<Tag> tags = new ArrayList<>();
tags.add(tag1);
tags.add(tag2);
ossClient.setObjectTagging(bucketName, key, tags);

获取对象标签

以下代码用于设置对象标签:

// Endpoint以华北三为例,其它Region请按实际情况填写。
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";
String bucketName = "<yourBucketName>";
String key = "<yourObjectName>";

//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);

//获取对象标签
SetTaggingRequest bucketTagging = ossClient.getObjectTagging(bucketName, key);
System.out.println("Getting object tags: " + bucketTagging);

删除对象标签

以下代码用于设置对象标签:

// Endpoint以华北三为例,其它Region请按实际情况填写。
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";
String bucketName = "<yourBucketName>";
String key = "<yourObjectName>";

//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);

//删除对象标签
ossClient.deleteObjectTagging(bucketName, key);