Attach tags
Call TagResources to attach tags to SLS resources.
Prerequisites
Simple Log Service is activated. For more information, see Enable Log Service.
Simple Log Service SDK for Java is initialized. For more information, see Initialize Simple Log Service SDK for Java.
Parameters
Request parameters
|
Parameter |
Type |
Required |
Description |
Example |
|
resourceType |
String |
Yes |
Resource type. Supported types:
|
project |
|
resourceId |
List<String> |
Yes |
The resource ID. You can specify and add tags to only one resource. When resourceType has different enumeration values, the parameters corresponding to resourceId are as follows:
|
ali-test-project |
|
tags |
List<Tag> |
Yes |
Tag list. Maximum 20 key-value pairs per request. |
[Tag{key='key1', value='value1'}, Tag{key='key2', value='value2'}] |
Tag parameters:
|
Parameter |
Type |
Required |
Description |
Example |
|
key |
String |
Yes |
Tag key. Requirements:
|
key1 |
|
value |
String |
Yes |
Tag value. Requirements:
|
value1 |
Response parameters
For information about the response parameters, see TagResources.
Sample code
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Tag;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.TagResourcesRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TagResourcesExample {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
static String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Specify a Simple Log Service endpoint. In this example, the endpoint of the China (Hangzhou) region is used. Replace this parameter value with your actual endpoint.
static String host = "cn-hangzhou.log.aliyuncs.com";
// Initialize the Simple Log Service client
static Client client = new Client(host, accessId, accessKey);
private static final String RESOURCE_TYPE = "project";
private static final String RESOURCE_ID = "ali-peoject-test1";
public static void main(String[] args) {
// Create a list of tags and add a tag object.
List<Tag> tags = new ArrayList<>();
tags.add(new Tag("key1", "value1"));
// Create a TagResourcesRequest object and specify the resource type, resource ID, and tags.
TagResourcesRequest request = new TagResourcesRequest(RESOURCE_TYPE, Collections.singletonList(RESOURCE_ID), tags);
try {
// Invoke the tagResources method to tag the resource.
client.tagResources(request);
} catch (LogException error) {
// Print the error message.
System.out.println("Error message :" + error.GetErrorMessage());
}
}
}
References
-
For more information about the APIs for managing tags, see the following topics: