本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
事件訊息結構
Amazon S3 傳送以發佈事件的通知訊息為 JSON 格式。
如需設定事件通知的一般概觀和指示,請參閱 Amazon S3 事件通知。
此範例顯示事件通知 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,", "ownerIdentity":{ "principalId":"amzn-s3-demo-bucketThe 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索引鍵值參考事件通知類型的清單,但不包含字s3:首。 -
userIdentity索引鍵值參考造成事件的 AWS Identity and Access Management (IAM) 資源 (使用者、角色、群組等) 的唯一 ID。如需每個 IAM 識別字首 (例如 AIDA、AROA、AGPA) 的定義,以及如何取得唯一識別符的資訊,請參閱《IAM 使用者指南》中的唯一識別符。 -
如果您想要追蹤請求,則
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 Retail (Amazon.com) 客戶 ID。此 ID 值不會再使用,只會為了回溯相容性而維護。 -
sequencer索引鍵值提供判斷事件順序的方法。事件通知不保證按事件發生的相同順序抵達。不過,來自建立物件 (PUT請求) 和刪除物件之事件的通知會包含sequencer。您可以使用此值來判斷指定物件金鑰的事件順序。如果比較
sequencer字串和相同物件金鑰的兩項事件通知,sequencer十六進位值較大的事件通知是發生較晚的事件。如果使用事件通知維護個別的 Amazon S3 物件資料庫或索引,則建議您在處理每項事件通知時,比較並存放sequencer值。請注意以下內容:
-
您無法使用
sequencer金鑰值來判斷不同物件金鑰上事件的順序。 -
sequencer字串的長度可能不同。因此,若要比較這些值,請先用滑鼠左鍵將較短的值加上零,然後執行詞典比較。
-
-
只有
s3:ObjectRestore:Completed事件才會顯示glacierEventData金鑰值。 -
restoreEventData索引鍵值包含與您的還原請求相關的屬性。 -
只有複寫事件才會顯示
replicationEventData金鑰值。 -
只有 S3 Intelligent-Tiering 事件才會顯示
intelligentTieringEventData金鑰值。 -
只有 S3 生命週期轉換事件才會顯示
lifecycleEventData金鑰值。
範例訊息
以下是 Amazon S3 事件通知訊息的範例。
Amazon S3 測試訊息
您在儲存貯體上設定事件通知後,Amazon S3 會傳送下列測試訊息。
{ "Service":"Amazon S3", "Event":"s3:TestEvent", "Time":"2014-10-13T15:57:02.089Z", "Bucket":"", "RequestId":"5582815E1AEA5ADF", "HostId":"8cLeGAmw098X5cv4Zkwcmo8vvZa3eH3eKxsPzbB9wrR+YstdA6Knx4Ip8EXAMPLE" }amzn-s3-demo-bucket
注意
s3:TestEvent 訊息使用的格式與一般 S3 事件通知不同。與其他使用先前所示Records陣列結構的事件通知不同,測試事件使用簡化格式搭配直接欄位。實作事件處理時,請確定您的程式碼可以區分和正確處理這兩種訊息格式。
使用 PUT 請求建立物件時的範例訊息
以下是 Amazon S3 傳送以發佈 s3:ObjectCreated:Put 事件的訊息範例。
{ "Records":[ { "eventVersion":"2.1", "eventSource":"aws:s3", "awsRegion":"us-west-2", "eventTime":"1970-01-01T00:00:00.000Z", "eventName":"ObjectCreated:Put", "userIdentity":{ "principalId":"AIDAJDPLRKLG7UEXAMPLE" }, "requestParameters":{ "sourceIPAddress":"172.16.0.1" }, "responseElements":{ "x-amz-request-id":"C3D13FE58DE4C810", "x-amz-id-2":"FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD" }, "s3":{ "s3SchemaVersion":"1.0", "configurationId":"testConfigRule", "bucket":{ "name":"amzn-s3-demo-bucket", "ownerIdentity":{ "principalId":"A3NL1KOZZKExample" }, "arn":"arn:aws:s3:::amzn-s3-demo-bucket" }, "object":{ "key":"HappyFace.jpg", "size":1024, "eTag":"d41d8cd98f00b204e9800998ecf8427e", "versionId":"096fKKXTRTtl3on89fVO.nfljtsv6qko", "sequencer":"0055AED6DCD90281E5" } } } ] }