使用物件金鑰名稱篩選來設定事件通知 - Amazon Simple Storage Service

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

使用物件金鑰名稱篩選來設定事件通知

設定 Amazon S3 事件通知時,您必須指定哪些支援的 Amazon S3 事件類型會導致 Amazon S3 傳送通知。如果您未指定的事件類型在 S3 儲存貯體中發生,則 Amazon S3 不會傳送通知。

您可以設定按物件金鑰名稱的字首和尾碼篩選通知。例如,您可以設定組態,只在將副檔名為「.jpg」的映像檔案新增至儲存貯體時才傳送通知。或者,您也可以擁有一個組態,在將具有前置詞 "images/" 的物件新增至值區時,將具有 "logs/" 前綴的物件傳送至 AWS Lambda 函數時,將通知傳送至 Amazon SNS 主題。

注意

萬用字元 ("*") 不能在篩選條件中用作前綴或後綴。如果您的字首或字尾包含空格,則必須將其取代為 "+" 字元。如果您在字首或字尾的值中使用任何其他特殊字元,則必須以 URL 編碼 (百分比編碼) 格式將其輸入。如需在事件通知的字首或字尾中使用時,必須轉換為 URL 編碼格式之特殊字元的完整清單,請參閱 安全字元

您可以設定通知組態,在 Amazon S3 主控台使用物件金鑰名稱篩選。您可以透過 AWS 開發套件或其餘 API 直接使用 Amazon S3 API 來執行此操作。如需使用主控台 UI 在儲存貯體上設定通知組態的相關資訊,請參閱「使用 Amazon S3 主控台啟用和設定事件通知」。

Amazon S3 會將通知組態儲存為 XML,放在與儲存貯體相關聯的通知子資源中,如 使用 Amazon SQS、Amazon SNS 和 Lambda 所述。您必須使用 Filter XML 結構定義規則,依物件金鑰名稱的字首或尾碼篩選通知。如需 Filter XML 結構的詳細資訊,請參閱 Amazon Simple Storage Service API 參考中的 PUT 儲存貯體通知

使用 Filter 的通知組態無法定義重疊字首、重疊尾碼或字首和尾碼重疊的篩選規則。下列小節提供使用物件金鑰名稱篩選的有效通知組態範例。它們也包含因為字首和尾碼重疊而無效的通知組態範例。

以物件金鑰名稱篩選的有效通知組態範例

下列通知組態包含的佇列組態,可識別 Amazon SQS 佇列,讓 Amazon S3 將事件發佈成 s3:ObjectCreated:Put 類型。只要字首為 images/ 和尾碼為 jpg 的物件 PUT 到儲存貯體,就會發佈事件。

<NotificationConfiguration> <QueueConfiguration> <Id>1</Id> <Filter> <S3Key> <FilterRule> <Name>prefix</Name> <Value>images/</Value> </FilterRule> <FilterRule> <Name>suffix</Name> <Value>jpg</Value> </FilterRule> </S3Key> </Filter> <Queue>arn:aws:sqs:us-west-2:444455556666:s3notificationqueue</Queue> <Event>s3:ObjectCreated:Put</Event> </QueueConfiguration> </NotificationConfiguration>

下列通知組態有多個非重疊字首。組態的定義是:images/ 資料夾中的 PUT 請求通知排入佇列 A,而 logs/ 資料夾中的 PUT 請求通知排入佇列 B。

<NotificationConfiguration> <QueueConfiguration> <Id>1</Id> <Filter> <S3Key> <FilterRule> <Name>prefix</Name> <Value>images/</Value> </FilterRule> </S3Key> </Filter> <Queue>arn:aws:sqs:us-west-2:444455556666:sqs-queue-A</Queue> <Event>s3:ObjectCreated:Put</Event> </QueueConfiguration> <QueueConfiguration> <Id>2</Id> <Filter> <S3Key> <FilterRule> <Name>prefix</Name> <Value>logs/</Value> </FilterRule> </S3Key> </Filter> <Queue>arn:aws:sqs:us-west-2:444455556666:sqs-queue-B</Queue> <Event>s3:ObjectCreated:Put</Event> </QueueConfiguration> </NotificationConfiguration>

下列通知組態有多個非重疊尾碼。組態的定義是:所有近期新增至儲存貯體的 .jpg 映像都由 Lambda cloud-function-A 處理,而所有近期新增的 .png 影像則由 cloud-function-B 處理。.png.jpg 字尾即使最後一個字母相同也不重疊。如果指定的字串可以這兩個尾碼結束,則兩個尾碼視為重疊。字串的結尾不能是 .png.jpg,所以範例組態中的字尾不是重疊的字尾。

<NotificationConfiguration> <CloudFunctionConfiguration> <Id>1</Id> <Filter> <S3Key> <FilterRule> <Name>suffix</Name> <Value>.jpg</Value> </FilterRule> </S3Key> </Filter> <CloudFunction>arn:aws:lambda:us-west-2:444455556666:cloud-function-A</CloudFunction> <Event>s3:ObjectCreated:Put</Event> </CloudFunctionConfiguration> <CloudFunctionConfiguration> <Id>2</Id> <Filter> <S3Key> <FilterRule> <Name>suffix</Name> <Value>.png</Value> </FilterRule> </S3Key> </Filter> <CloudFunction>arn:aws:lambda:us-west-2:444455556666:cloud-function-B</CloudFunction> <Event>s3:ObjectCreated:Put</Event> </CloudFunctionConfiguration> </NotificationConfiguration>

使用 Filter 的通知組態無法定義相同事件類型的重疊字首篩選規則。如果重疊字首搭配的尾碼不重疊,則只能這麼做。下列範例組態示範如何將以常見字首但不重疊尾碼建立的物件,傳送至不同的目標。

<NotificationConfiguration> <CloudFunctionConfiguration> <Id>1</Id> <Filter> <S3Key> <FilterRule> <Name>prefix</Name> <Value>images</Value> </FilterRule> <FilterRule> <Name>suffix</Name> <Value>.jpg</Value> </FilterRule> </S3Key> </Filter> <CloudFunction>arn:aws:lambda:us-west-2:444455556666:cloud-function-A</CloudFunction> <Event>s3:ObjectCreated:Put</Event> </CloudFunctionConfiguration> <CloudFunctionConfiguration> <Id>2</Id> <Filter> <S3Key> <FilterRule> <Name>prefix</Name> <Value>images</Value> </FilterRule> <FilterRule> <Name>suffix</Name> <Value>.png</Value> </FilterRule> </S3Key> </Filter> <CloudFunction>arn:aws:lambda:us-west-2:444455556666:cloud-function-B</CloudFunction> <Event>s3:ObjectCreated:Put</Event> </CloudFunctionConfiguration> </NotificationConfiguration>

無效字首和尾碼重疊的通知組態範例

在大多數情況下,使用 Filter 的通知組態無法定義相同事件類型的重疊字首、重疊尾碼或字首和尾碼重疊組合的篩選規則。只要字首不重疊就可以有重疊的前綴。如需範例,請參閱使用物件金鑰名稱篩選來設定事件通知

不同的事件類型可以使用重疊的物件金鑰名稱篩選。例如,您可以建立通知組態,在 image/ 事件類型使用字首 ObjectCreated:Put,在 image/ 事件類型使用字首 ObjectRemoved:*

當您使用 Amazon S3 主控台或 API 時,如果嘗試儲存的通知組態其相同事件類型具無效的重疊名稱篩選條件,就會發生錯誤。本節示範因為重疊名稱篩而無效的通知組態範例。

現有的全部通知組態,都假設分別擁有與任何其他字首和尾碼符合的預設字首和尾碼。下列通知組態因有重疊的字首而無效。明確地說,根字首會與任何其他字首重疊。在本範例中,如果使用尾碼而不使用字首,也是同樣的情況。根尾碼會與任何其他尾碼重疊。

<NotificationConfiguration> <TopicConfiguration> <Topic>arn:aws:sns:us-west-2:444455556666:sns-notification-one</Topic> <Event>s3:ObjectCreated:*</Event> </TopicConfiguration> <TopicConfiguration> <Topic>arn:aws:sns:us-west-2:444455556666:sns-notification-two</Topic> <Event>s3:ObjectCreated:*</Event> <Filter> <S3Key> <FilterRule> <Name>prefix</Name> <Value>images</Value> </FilterRule> </S3Key> </Filter> </TopicConfiguration> </NotificationConfiguration>

下列通知組態因有重疊的字尾而無效。如果指定的字串可以這兩個尾碼結束,則兩個尾碼視為重疊。字串的結尾可以使用 jpgpg。所以,字尾會重疊。字首也是一樣。如果指定的字串可以這兩個字首開始,則兩個字首視為重疊。

<NotificationConfiguration> <TopicConfiguration> <Topic>arn:aws:sns:us-west-2:444455556666:sns-topic-one</Topic> <Event>s3:ObjectCreated:*</Event> <Filter> <S3Key> <FilterRule> <Name>suffix</Name> <Value>jpg</Value> </FilterRule> </S3Key> </Filter> </TopicConfiguration> <TopicConfiguration> <Topic>arn:aws:sns:us-west-2:444455556666:sns-topic-two</Topic> <Event>s3:ObjectCreated:Put</Event> <Filter> <S3Key> <FilterRule> <Name>suffix</Name> <Value>pg</Value> </FilterRule> </S3Key> </Filter> </TopicConfiguration> </NotificationConfiguration

下列通知組態因有重疊的字首和字尾而無效。

<NotificationConfiguration> <TopicConfiguration> <Topic>arn:aws:sns:us-west-2:444455556666:sns-topic-one</Topic> <Event>s3:ObjectCreated:*</Event> <Filter> <S3Key> <FilterRule> <Name>prefix</Name> <Value>images</Value> </FilterRule> <FilterRule> <Name>suffix</Name> <Value>jpg</Value> </FilterRule> </S3Key> </Filter> </TopicConfiguration> <TopicConfiguration> <Topic>arn:aws:sns:us-west-2:444455556666:sns-topic-two</Topic> <Event>s3:ObjectCreated:Put</Event> <Filter> <S3Key> <FilterRule> <Name>suffix</Name> <Value>jpg</Value> </FilterRule> </S3Key> </Filter> </TopicConfiguration> </NotificationConfiguration>