

# 事件消息结构
<a name="notification-content-structure"></a>

Amazon S3 为发布事件而发送的通知消息采用 JSON 格式。

有关配置事件通知的一般概述和说明，请参阅 [Amazon S3 事件通知](EventNotifications.md)。

此示例说明了事件通知 JSON 结构的*版本 2.1*。Amazon S3 使用了这个事件结构的*版本 2.1*、*2.2* 和 *2.3*。Amazon S3 使用版本 2.2 进行跨区域复制事件通知。它将 2.3 版本用于 S3 生命周期、S3 Intelligent-Tiering、对象 ACL、对象标记和对象恢复删除事件。这些版本包含特定于这些操作的额外信息。版本 2.2 和版本 2.3 与版本 2.1 兼容，Amazon S3 当前将该版本用于所有其他事件类型。

```
{  
   "Records":[  
      {  
         "eventVersion":"2.1",
         "eventSource":"aws:s3",
         "awsRegion":"us-west-2",
         "eventTime":"The time, in ISO-8601 format (for example, 1970-01-01T00:00:00.000Z) when Amazon S3 finished processing the request",
         "eventName":"The event type",
         "userIdentity":{  
            "principalId":"The unique ID of the IAM resource that caused the event"
         },
         "requestParameters":{  
            "sourceIPAddress":"The IP address where the request came from"
         },
         "responseElements":{  
            "x-amz-request-id":"The Amazon S3 generated request ID",
            "x-amz-id-2":"The Amazon S3 host that processed the request"
         },
         "s3":{  
            "s3SchemaVersion":"1.0",
            "configurationId":"The ID found in the bucket notification configuration",
            "bucket":{  
               "name":"The name of the bucket, for example, amzn-s3-demo-bucket",
               "ownerIdentity":{  
                  "principalId":"The Amazon retail customer ID of the bucket owner"
               },
               "arn":"The bucket Amazon Resource Name (ARN)"
            },
            "object":{  
               "key":"The object key name",
               "size":"The object size in bytes (as a number)",
               "eTag":"The object entity tag (ETag)",
               "versionId":"The object version if the bucket is versioning-enabled; null or not present if the bucket isn't versioning-enabled",
               "sequencer": "A string representation of a hexadecimal value used to determine event sequence; only used with PUT and DELETE requests"
            }
         },
         "glacierEventData": {
            "restoreEventData": {
               "lifecycleRestorationExpiryTime": "The time, in ISO-8601 format (for example, 1970-01-01T00:00:00.000Z), when the temporary copy of the restored object expires",
               "lifecycleRestoreStorageClass": "The source storage class for restored objects"
            }
         }
      }
   ]
}
```

注意，以下是关于事件消息结构：
+ `eventVersion` 键值包含 `major`.`minor` 格式的主要版本和次要版本。

  如果 Amazon S3 对不向后兼容的事件结构进行更改，则主要版本将递增。这包括删除已存在的 JSON 字段或更改字段内容的表示方式（例如，日期格式）。

  如果 Amazon S3 向事件结构添加新字段，则次要版本将递增。如果为某些或所有现有事件提供了新信息，则可能会发生这种情况 如果仅提供有关新引入的事件类型的新信息，则可能会发生这种情况。要与事件结构的新次要版本保持兼容，我们建议应用程序忽略新字段。

  如果引入了新的事件类型，但事件的结构未经修改，则事件版本不会更改。

  为确保您的应用程序可以正确解析事件结构，我们建议您对主要版本号进行相等比较。为了确保您的应用程序所期望的字段存在，我们还建议对次要版本进行大于或等于比较。
+ `eventName` 键值引用[事件通知类型](https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-how-to-event-types-and-destinations.html)列表，但不包含 `s3:` 前缀。
+ `userIdentity` 键值引用导致事件的 AWS Identity and Access Management（IAM）资源（用户、角色、组等）的唯一 ID。有关每个 IAM 标识前缀（如 AIDA、AROA、AGPA 等）的定义和有关如何获取唯一标识符的信息，请参阅《IAM 用户指南》**中的[唯一标识符](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html##identifiers-unique-ids)。
+ 如果您希望通过联系 `responseElements` 来跟踪请求，则 AWS 支持 键值很有用。`x-amz-request-id` 和 `x-amz-id-2` 都可帮助 Amazon S3 跟踪单个请求。这些值与 Amazon S3 为响应您启动事件的请求而返回的值相同。因此，您可以使用这些值将事件与请求匹配。
+ `s3` 键值提供事件中涉及的存储桶和对象的相关信息。对象键名称值进行了 URL 编码。例如，`red flower.jpg` 改为 `red+flower.jpg`。（Amazon S3 返回“`application/x-www-form-urlencoded`”作为响应中的内容类型。）

  `ownerIdentity` 键值对应于存储桶拥有者的 Amazon 零售（Amazon.com）客户 ID。不再使用此 ID 值，保留此值仅是为了向后兼容。
+ `sequencer` 键值提供了一种用于确定事件顺序的方法。无法保证事件通知按事件发生的顺序到达。然而，来自创建对象（`PUT` 请求）和删除对象的事件的通知将包含 `sequencer`。您可以使用此值来确定给定对象键的事件的顺序。

  如果将来自同一对象键上的两个事件通知的 `sequencer` 字符串进行比较，就会发现 `sequencer` 十六进制值较大的事件通知是后发生的事件。如果您正在使用事件通知来维护 Amazon S3 对象的单独数据库或索引，我们建议您在处理每个事件通知时比较和存储 `sequencer` 值。

  请注意以下几点：
  + 您不能使用 `sequencer` 键值来确定不同对象键上事件的顺序。
  + `sequencer` 字符串可以有不同的长度。因此，为了比较这些值，首先要用零填补较短值的左侧，然后进行字母表顺序比较。
+ `glacierEventData` 键值只对 `s3:ObjectRestore:Completed` 事件可见。
+ `restoreEventData` 键值包含与还原请求相关的属性。
+ `replicationEventData` 键值只对复制事件可见。
+ `intelligentTieringEventData` 键值只对 S3 Intelligent-Tiering 可见。
+ `lifecycleEventData` 键值只对 S3 生命周期转换事件可见。

## 示例消息
<a name="notification-content-structure-examples"></a>

以下是 Amazon S3 事件通知消息的示例。

**Amazon S3 测试消息**  
当您在存储桶上配置事件通知时，Amazon S3 会发送以下测试消息。

```
1. {  
2.    "Service":"Amazon S3",
3.    "Event":"s3:TestEvent",
4.    "Time":"2014-10-13T15:57:02.089Z",
5.    "Bucket":"amzn-s3-demo-bucket",
6.    "RequestId":"5582815E1AEA5ADF",
7.    "HostId":"8cLeGAmw098X5cv4Zkwcmo8vvZa3eH3eKxsPzbB9wrR+YstdA6Knx4Ip8EXAMPLE"
8. }
```

**注意**  
`s3:TestEvent` 消息使用的格式与常规 S3 事件通知不同。与其它使用之前显示的 `Records` 数组结构的事件通知不同，测试事件使用带有直接字段的简化格式。在实施事件处理时，请确保您的代码能够区分和正确地处理这两种消息格式。

**使用 `PUT` 请求创建对象时的示例消息**  
以下是 Amazon S3 发送以发布 `s3:ObjectCreated:Put` 事件的消息示例。

```
 1. {  
 2.    "Records":[  
 3.       {  
 4.          "eventVersion":"2.1",
 5.          "eventSource":"aws:s3",
 6.          "awsRegion":"us-west-2",
 7.          "eventTime":"1970-01-01T00:00:00.000Z",
 8.          "eventName":"ObjectCreated:Put",
 9.          "userIdentity":{  
10.             "principalId":"AIDAJDPLRKLG7UEXAMPLE"
11.          },
12.          "requestParameters":{  
13.             "sourceIPAddress":"172.16.0.1"
14.          },
15.          "responseElements":{  
16.             "x-amz-request-id":"C3D13FE58DE4C810",
17.             "x-amz-id-2":"FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
18.          },
19.          "s3":{  
20.             "s3SchemaVersion":"1.0",
21.             "configurationId":"testConfigRule",
22.             "bucket":{  
23.                "name":"amzn-s3-demo-bucket",
24.                "ownerIdentity":{  
25.                   "principalId":"A3NL1KOZZKExample"
26.                },
27.                "arn":"arn:aws:s3:::amzn-s3-demo-bucket"
28.             },
29.             "object":{  
30.                "key":"HappyFace.jpg",
31.                "size":1024,
32.                "eTag":"d41d8cd98f00b204e9800998ecf8427e",
33.                "versionId":"096fKKXTRTtl3on89fVO.nfljtsv6qko",
34.                "sequencer":"0055AED6DCD90281E5"
35.             }
36.          }
37.       }
38.    ]
39. }
```

