

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 为资源添加标签
<a name="tagging"></a>

标签是一种自定义元数据标签，您可以将其添加到资源中，以便更轻松地在搜索中识别、整理和查找。标签由两个独立部分组成：标签键和标签值。这被称为键值对。

标签键通常表示较大的类别，而标签值则表示该类别的子集。例如，您可能拥有*标签键=颜色*和*标签值=蓝色*，这样将生成键值对 `Color:Blue`。请注意，您可以将标签的值设为空的字符串，但是不能将其设为空值。省略标签值与使用空字符串效果相同。

**提示**  
AWS 账单与成本管理 可以使用标签将账单分为动态类别。例如，如果您添加标签来代表公司内的不同部门，例如 `Department:Sales` 或 `Department:Legal`，则 AWS 可以为您提供每个部门的成本分配。

在 Amazon Transcribe 中，您可以为以下资源添加标签：
+ 转录作业
+ 医疗转录作业
+ 通话分析通话后转录作业
+ 自定义词汇表
+ 自定义医学词汇表
+ 自定义词汇表过滤器
+ 通话分析类别
+ 自定义语言模型

标签键的长度最大可以为 128 个字符，标签值的长度最大可以为 256 个字符；两者均区分大小写。Amazon Transcribe 对每个资源支持最多 50 个标签。对于给定资源，每个标签键必须是唯一的，只有一个值。请注意，您的标签不能以 `aws:` 开头，因为 AWS 会为系统生成的标签保留此前缀。您无法添加、修改或删除 `aws:*` 标签，而且它们不会计入每个资源的标签数的限制。

**特定于资源标记的 API 操作**  
 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_ListTagsForResource.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_ListTagsForResource.html), [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_TagResource.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_TagResource.html), [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_UntagResource.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_UntagResource.html)   
要使用标签 API，您必须在请求中包含 Amazon 资源名称 (ARN)。ARN 的格式为 `arn:partition:service:region:account-id:resource-type/resource-id`。例如，与转录作业关联的 ARN 可能如下所示：`arn:aws:transcribe:us-west-2:111122223333:transcription-job/my-transcription-job-name`

要了解有关标记（包括最佳实践）的更多信息，请参阅[标记 AWS 资源](https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html)。

## 基于标签的访问控制
<a name="tagging-access-control"></a>

您可以使用标签来控制 AWS 账户内的访问权限。对于基于标签的访问控制，您需要在 IAM 策略的条件元素中提供标签信息。然后，您可以使用标签及其关联的标签条件键控制对以下内容的访问：
+ **资源：**根据您为 Amazon Transcribe 资源分配的标签控制对这些资源的访问。
  + 请使用 `aws:ResourceTag/key-name` 条件键指定必须将哪个标签键值对附加到资源。
+ **请求：**控制哪些标签可以传入请求。
  + 使用 `aws:RequestTag/key-name` 条件键来指定可在 IAM 用户或角色中添加、修改或删除的标签。
+ **授权流程：**控制授权过程任何部分的基于标签的访问权限。
  +  使用 `aws:TagKeys/` 条件键控制是否可以在资源或请求中或者由主体使用特定标签键。在这种情况下，键值无关紧要。

有关基于标签的访问控制策略的示例，请参阅[基于标签查看转录作业](security_iam_id-based-policy-examples.md#tagging-transcription-policy)。

有关基于标签的访问控制的更多详细信息，请参阅[使用标签控制对 AWS 资源的访问](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_tags.html)。

## 向 Amazon Transcribe 资源添加标签
<a name="tagging-how-to"></a>

您可以在运行 Amazon Transcribe 作业之前或之后添加标签。使用现有的 **Create\$1** 和 **Start\$1** API，您可以在转录请求中添加标签。

您可以使用 **AWS 管理控制台**、**AWS CLI** 或 **AWS SDK** 添加、修改或删除标签；有关示例，请参阅以下内容：

### AWS 管理控制台
<a name="tagging-console"></a>

1. 登录到 [AWS 管理控制台](https://console.aws.amazon.com/transcribe/)。

1. 在导航窗格中，选择**转录作业**，然后选择**创建作业**（右上角）。这将打开**指定作业详细信息**页面。

1. 滚动到**指定作业详细信息**页面底部，找到**标签 - *可选***框，然后选择**添加新标签**。  
![\[Amazon Transcribe 控制台屏幕截图：“指定作业详细信息”页面。\]](http://docs.aws.amazon.com/zh_cn/transcribe/latest/dg/images/add-new-tag.png)

1. 在**键**字段和（可选）**值**字段中输入信息。  
![\[Amazon Transcribe 控制台屏幕截图：“标签”面板，显示示例标签。\]](http://docs.aws.amazon.com/zh_cn/transcribe/latest/dg/images/add-new-tag-color.png)

1. 在**指定作业详细信息**页面上填写要包含的任何其它字段，然后选择**下一步**。此时您将会看到**配置作业 - *可选***页面。

   选择**创建作业**以运行您的转录作业。

1. 您可以通过导航到**转录作业**页面，选择转录作业，然后滚动到该作业信息页面的底部，来查看与转录作业相关的标签。如果要编辑标签，可以选择**管理标签**。  
![\[Amazon Transcribe 控制台屏幕截图：转录作业信息页面中的“标签”面板。\]](http://docs.aws.amazon.com/zh_cn/transcribe/latest/dg/images/view-tags.png)

### AWS CLI
<a name="tagging-cli"></a>

此示例使用 [start-transcription-job](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/transcribe/start-transcription-job.html) 命令和 `Tags` 参数。有关更多信息，请参阅 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html) 和 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Tag.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Tag.html)。

```
aws transcribe start-transcription-job \
--region us-west-2 \
--transcription-job-name my-first-transcription-job \
--media MediaFileUri=s3://amzn-s3-demo-bucket/my-input-files/my-media-file.flac \
--output-bucket-name amzn-s3-demo-bucket \
--output-key my-output-files/ \
--language-code en-US \
--tags Key=color,Value=blue Key=shape,Value=square
```

以下是另一个使用 [start-transcription-job](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/transcribe/start-transcription-job.html) 命令的示例，以及一个将标签添加到该作业的请求正文。

```
aws transcribe start-transcription-job \
--region us-west-2 \
--cli-input-json file://filepath/my-first-tagging-job.json
```

*my-first-tagging-job.json* 文件包含以下请求正文。

```
{
  "TranscriptionJobName": "my-first-transcription-job",
  "Media": {
        "MediaFileUri": "s3://amzn-s3-demo-bucket/my-input-files/my-media-file.flac"
  },
  "OutputBucketName": "amzn-s3-demo-bucket",
  "OutputKey": "my-output-files/", 
  "LanguageCode": "en-US",
  "Tags": [ 
        {
           "Key": "color",
           "Value": "blue"
        },
        {
           "Key": "shape",
           "Value": "square"
        }
  ]
}
```

### 适用于 Python (Boto3) 的 AWS SDK
<a name="tagging-python-batch"></a>

以下示例使用 适用于 Python (Boto3) 的 AWS SDK 通过 [start\$1transcription\$1job](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/transcribe.html#TranscribeService.Client.start_transcription_job) 方法的 `Tags` 参数添加标签。有关更多信息，请参阅 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html) 和 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Tag.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Tag.html)。

有关使用 AWS SDK 的其它示例，包括特定特征、场景和跨服务示例，请参阅[使用 Amazon Transcribe 的代码示例 AWS SDKs](service_code_examples.md)一章。

```
from __future__ import print_function
import time
import boto3
transcribe = boto3.client('transcribe', 'us-west-2')
job_name = "my-first-transcription-job"
job_uri = "s3://amzn-s3-demo-bucket/my-input-files/my-media-file.flac"
transcribe.start_transcription_job(
    TranscriptionJobName = job_name,
    Media = {
        'MediaFileUri': job_uri
    },
    OutputBucketName = 'amzn-s3-demo-bucket',
    OutputKey = 'my-output-files/', 
    LanguageCode = 'en-US', 
    Tags = [
        {
            'Key':'color', 
            'Value':'blue'
        }
    ]
)

while True:
    status = transcribe.get_transcription_job(TranscriptionJobName = job_name)
    if status['TranscriptionJob']['TranscriptionJobStatus'] in ['COMPLETED', 'FAILED']:
        break
    print("Not ready yet...")
    time.sleep(5)
print(status)
```