

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

# 使用 Amazon SES API 监控您的使用情况统计数据
<a name="monitor-sending-activity-api"></a>

Amazon SES API 提供 `GetSendStatistics` 操作，用于返回有关服务使用情况的信息。我们建议您定期查看您的发送统计数据，以便根据需要进行调整。

当您调用 `GetSendStatistics` 操作时，您会收到表示您最近两周的发送活动的数据点的列表。此列表中的每个数据点代表一项为时 15 分钟的活动，并包含该时间段的以下信息：
+ 查无此人的邮件的数量
+ 投诉的数量
+ 传送尝试的次数 (对应于您已发送的电子邮件数)
+ 被拒绝的发送尝试的次数
+ 分析时段的时间戳

有关 `GetSendStatistics` 操作的完整描述，请参阅 [Amazon Simple Email Service API 参考](https://docs.aws.amazon.com/ses/latest/APIReference/GetSendStatistics.html)。

在本节中，您将找到以下主题：
+ [使用调用 `GetSendStatistics` API 操作 AWS CLI](#monitor-sending-activity-api-cli)
+ [以编程方式调用 `GetSendStatistics` 操作](#monitor-sending-activity-api-sdk)

## 使用调用 `GetSendStatistics` API 操作 AWS CLI
<a name="monitor-sending-activity-api-cli"></a>

调用 `GetSendStatistics` API 操作的最简单方法是使用 [AWS Command Line Interface](https://aws.amazon.com/cli) (AWS CLI)。

**要调用 `GetSendStatistics` API 操作，请使用 AWS CLI**

1. 如果您尚未这样做，请安装 AWS CLI。有关更多信息，请参阅《*AWS Command Line Interface 用户指南 AWS Command Line Interface*[》中的 “安装](https://docs.aws.amazon.com/cli/latest/userguide/installing.html)”。

1. 如果您尚未执行此操作，请将配置 AWS CLI 为使用您的 AWS 证书。有关更多信息，请参阅《*AWS Command Line Interface 用户指南 AWS CLI*[》中的 “配置](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)”。

1. 在命令行处，运行以下命令：

   ```
   aws ses get-send-statistics
   ```

   如果配置 AWS CLI 正确，您将看到以 JSON 格式发送统计信息的列表。每个 JSON 对象都包含 15 分钟时段内的聚合发送统计数据。

## 以编程方式调用 `GetSendStatistics` 操作
<a name="monitor-sending-activity-api-sdk"></a>

您也可以使用调用该`GetSendStatistics`操作 AWS SDKs。本节包括 for Go、PHP、Python 和 Ruby 的代码示例。 AWS SDKs 选择以下链接之一可查看相应语言的代码示例：
+ [适用于 Go 的 AWS SDK的代码示例](#code-example-getsendstatistics-golang)
+ [适用于 PHP 的 AWS SDK的代码示例](#code-example-getsendstatistics-php)
+ [AWS SDK for Python (Boto)的代码示例](#code-example-getsendstatistics-python)
+ [适用于 Ruby 的 AWS SDK的代码示例](#code-example-getsendstatistics-ruby)

**注意**  
这些代码示例假设您已创建 AWS 共享凭证文件，其中包含您的 AWS 访问密钥 ID、私有访问 AWS 密钥和首选 AWS 区域。有关更多信息，请参阅[共享凭证和配置文件](https://docs.aws.amazon.com/credref/latest/refdocs/creds-config-files.html)。

### `GetSendStatistics`使用拨打电话 适用于 Go 的 AWS SDK
<a name="code-example-getsendstatistics-golang"></a>

```
 1. package main
 2.     
 3. import (
 4.     "fmt"
 5.     
 6.     //go get github.com/aws/aws-sdk-go/...
 7.     "github.com/aws/aws-sdk-go/aws"
 8.     "github.com/aws/aws-sdk-go/aws/session"
 9.     "github.com/aws/aws-sdk-go/service/ses"
10.     "github.com/aws/aws-sdk-go/aws/awserr"
11. )
12.     
13. const (
14.     // Replace us-west-2 with the AWS Region you're using for Amazon SES.
15.     AwsRegion = "us-west-2"
16. )
17.     
18. func main() {
19.     
20.     // Create a new session and specify an AWS Region.
21.     sess, err := session.NewSession(&aws.Config{
22.         Region:aws.String(AwsRegion)},
23.     )
24.     
25.     // Create an SES client in the session.
26.     svc := ses.New(sess)
27.     input := &ses.GetSendStatisticsInput{}
28.     
29.     result, err := svc.GetSendStatistics(input)
30.     
31.     // Display error messages if they occur.
32.     if err != nil {
33.         if aerr, ok := err.(awserr.Error); ok {
34.             switch aerr.Code() {
35.             default:
36.                 fmt.Println(aerr.Error())
37.             }
38.         } else {
39.             // Print the error, cast err to awserr.Error to get the Code and
40.             // Message from an error.
41.             fmt.Println(err.Error())
42.         }
43.         return
44.     }
45.     
46.     fmt.Println(result)
47. }
```

### `GetSendStatistics`使用拨打电话 适用于 PHP 的 AWS SDK
<a name="code-example-getsendstatistics-php"></a>

```
 1. <?php
 2. 
 3. // Replace path_to_sdk_inclusion with the path to the SDK as described in 
 4. // http://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/basic-usage.html
 5. define('REQUIRED_FILE','path_to_sdk_inclusion');
 6.                                                   
 7. // Replace us-west-2 with the AWS Region you're using for Amazon SES.
 8. define('REGION','us-west-2'); 
 9. 
10. require REQUIRED_FILE;
11. 
12. use Aws\Ses\SesClient;
13. 
14. $client = SesClient::factory(array(
15.     'version'=> 'latest',     
16.     'region' => REGION
17. ));
18. 
19. try {
20.      $result = $client->getSendStatistics([]);
21. 	 echo($result);
22. } catch (Exception $e) {
23.      echo($e->getMessage()."\n");
24. }
25. 
26. ?>
```

### `GetSendStatistics`使用拨打电话 AWS SDK for Python (Boto)
<a name="code-example-getsendstatistics-python"></a>

```
 1. import boto3 #pip install boto3
 2. import json
 3. from botocore.exceptions import ClientError
 4. 
 5. client = boto3.client('ses')
 6. 
 7. try:
 8.     response = client.get_send_statistics(
 9. )
10. except ClientError as e:
11.     print(e.response['Error']['Message'])
12. else:
13.     print(json.dumps(response, indent=4, sort_keys=True, default=str))
```

### `GetSendStatistics`使用拨打电话 适用于 Ruby 的 AWS SDK
<a name="code-example-getsendstatistics-ruby"></a>

```
 1. require 'aws-sdk' # gem install aws-sdk
 2. require 'json'
 3. 
 4. # Replace us-west-2 with the AWS Region you're using for Amazon SES.
 5. awsregion = "us-west-2"
 6. 
 7. # Create a new SES resource and specify a region
 8. ses = Aws::SES::Client.new(region: awsregion)
 9. 
10. begin
11. 
12.   resp = ses.get_send_statistics({
13.   })
14.   puts JSON.pretty_generate(resp.to_h)
15. 
16. # If something goes wrong, display an error message.
17. rescue Aws::SES::Errors::ServiceError => error
18.   puts error
19. 
20. end
```