本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
設定 Amazon SQS 佇列的標籤
使用成本分配標籤來幫助組織和識別您的 Amazon SQS 佇列。下列範例示範如何使用 AWS SDK for Java設定標籤。如需詳細資訊,請參閱 Amazon SQS成本分配標籤。
在執行範例程式碼之前,請確定您已設定 AWS 認證。如需詳細資訊,請參閱開發AWS SDK for Java 2.x 人員指南中的設定 AWS 認證和開發區域。
列出標籤
若要列出佇列的標籤,請使用 ListQueueTags
方法。
// Create an SqsClient for the specified region. SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_1).build(); // Get the queue URL. String queueName = "MyStandardQ1"; GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build()); String queueUrl = getQueueUrlResponse.queueUrl(); // Create the ListQueueTagsRequest. final ListQueueTagsRequest listQueueTagsRequest = ListQueueTagsRequest.builder().queueUrl(queueUrl).build(); // Retrieve the list of queue tags and print them. final ListQueueTagsResponse listQueueTagsResponse = sqsClient.listQueueTags(listQueueTagsRequest); System.out.println(String.format("ListQueueTags: \tTags for queue %s are %s.\n", queueName, listQueueTagsResponse.tags() ));
新增或更新標籤
若要新增或更新佇列的標籤值,請使用 TagQueue
方法。
// Create an SqsClient for the specified Region. SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_1).build(); // Get the queue URL. String queueName = "MyStandardQ1"; GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build()); String queueUrl = getQueueUrlResponse.queueUrl(); // Build a hashmap of the tags. final HashMap<String, String> addedTags = new HashMap<>(); addedTags.put("Team", "Development"); addedTags.put("Priority", "Beta"); addedTags.put("Accounting ID", "456def"); //Create the TagQueueRequest and add them to the queue. final TagQueueRequest tagQueueRequest = TagQueueRequest.builder() .queueUrl(queueUrl) .tags(addedTags) .build(); sqsClient.tagQueue(tagQueueRequest);
移除標籤
若要移除佇列的一個或多個標籤,請使用 UntagQueue
方法。下列範例會移除 Accounting ID
標籤。
// Create the UntagQueueRequest. final UntagQueueRequest untagQueueRequest = UntagQueueRequest.builder() .queueUrl(queueUrl) .tagKeys("Accounting ID") .build(); // Remove the tag from this queue. sqsClient.untagQueue(untagQueueRequest);