

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Amazon WorkSpaces 應用程式 API、 AWS SDK 或 AWS CLI 來處理標籤
<a name="basic-tagging-API-SDK-CLI"></a>

如果您使用 WorkSpaces 應用程式 API、 AWS 開發套件或 AWS 命令列界面 (AWS CLI)，您可以在建立新資源時使用下列 WorkSpaces 應用程式操作搭配 `tags` 參數來新增標籤。

**注意**  
您可以在標籤鍵和值內使用空格。若要在使用 CLI AWS 時指出空格，請使用 "\$1s" （不含引號）。


| 任務 | AWS CLI | API 作業 | 
| --- | --- | --- | 
| 為新的機群新增一或多個標籤 | [create-fleet](https://docs.aws.amazon.com/cli/latest/reference/appstream/create-fleet.html)  |  [CreateFleet](https://docs.aws.amazon.com/appstream2/latest/APIReference/API_CreateFleet.html#AppStream2-CreateFleet-request-Tags)  | 
| 為新的映像建置器新增一或多個標籤 | [create-imagebuilder](https://docs.aws.amazon.com/cli/latest/reference/appstream/create-imagebuilder.html) |  [CreateImageBuilder](https://docs.aws.amazon.com/appstream2/latest/APIReference/API_CreateImageBuilder.html#AppStream2-CreateImageBuilder-request-Tags)  | 
| 為新的堆疊新增一或多個標籤 |  [create-stack](https://docs.aws.amazon.com/cli/latest/reference/appstream/create-stack.html)  |  [CreateStack](https://docs.aws.amazon.com/appstream2/latest/APIReference/API_CreateStack.html#AppStream2-CreateStack-request-Tags)  | 

您可以使用下列 WorkSpaces 應用程式操作來新增、編輯、移除或列出現有資源的標籤：


| 任務 | AWS CLI | API 作業 | 
| --- | --- | --- | 
| 新增或覆寫資源的一或多個標籤 | [tag-resource](https://docs.aws.amazon.com/cli/latest/reference/appstream/tag-resource.html)  |  [TagResource](https://docs.aws.amazon.com/appstream2/latest/APIReference/API_TagResource.html)  | 
| 移除資源的一或多個標籤 | [untag-resource](https://docs.aws.amazon.com/cli/latest/reference/appstream/untag-resource.html) |  [UntagResource](https://docs.aws.amazon.com/appstream2/latest/APIReference/API_UntagResource.html)  | 
| 列出資源的一或多個標籤 |  [list-tags-for-resource](https://docs.aws.amazon.com/cli/latest/reference/appstream/list-tags-for-resource.html)  |  [ListTagsForResource](https://docs.aws.amazon.com/appstream2/latest/APIReference/API_ListTagsForResource.html)  | 

當您使用 WorkSpaces 應用程式 API、 AWS 開發套件或 AWS CLI 動作來新增、編輯、移除或列出現有 WorkSpaces 應用程式資源的標籤時，請使用其 Amazon Resource Name (ARN) 來指定資源。ARN 可唯一識別 AWS 資源，並使用下列一般語法。

```
arn:aws:appstream:region:account:resourceType/resourceName
```

***region***  
建立資源 AWS 的區域 （例如 `us-east-1`)。

***account***  
 AWS 帳戶 ID，不含連字號 （例如 `123456789012`)。

***resourceType***  
資源的類型。您可以標記下列 WorkSpaces 應用程式資源類型：`image-builder`、`fleet`、 `image`和 `stack`。

***resourceName***  
資源的名稱。

例如，您可以使用 CLI [describe-fleets ](https://docs.aws.amazon.com/cli/latest/reference/appstream/describe-fleets.html) AWS 命令來取得 WorkSpaces 應用程式機群的 ARN。複製以下命令。

```
aws appstream describe-fleets
```

針對包含單一名為 `TestFleet` 機群的環境，此資源的 ARN 在 JSON 輸出中看起來會如下所示。

```
"Arn": "arn:aws:appstream:us-east-1:123456789012:fleet/TestFleet"
```

在您取得此資源的 ARN 後，您可以使用 [tag-resource](https://docs.aws.amazon.com/cli/latest/reference/appstream/tag-resource.html) 命令來新增兩個標籤：

```
aws appstream tag-resource --resource arn:awsappstream:us-east-1:123456789012:fleet/TestFleet --tags Environment=Test,Department=IT
```

第一個標籤 (`Environment=Test`) 指出機群位於測試環境。第二個標籤 (`Department=IT`) 則指出機群位於 IT 部門。

您可以使用以下命令來列出您新增到機群的兩個標籤。

```
aws appstream list-tags-for-resource --resource arn:aws:appstream:us-east-1:123456789012:fleet/TestFleet
```

針對此範例，JSON 輸出看起來會如下所示：

```
{
    "Tags": {
       "Environment" : "Test",
       "Department" : "IT"
    }
}
```