S3 事件通知 API 从版本 1 到版本 2 的更改 - AWS SDK for Java 2.x

S3 事件通知 API 从版本 1 到版本 2 的更改

本主题详细说明了 S3 事件通知 API 从适用于 Java 的 AWS SDK 版本 1.x(v1)到版本 2(v2)的更改。

高级别更改

结构变化

V1 对 EventNotificationRecord 类型及其属性使用静态内部类,而 v2 对 EventNotificationRecord 类型使用单独的公共类。

命名约定更改

在 v1 中,属性类名称包括后缀 Entity,而 v2 为了更简单的命名而省略了此后缀:例如,使用 eventData 而不是使用 eventDataEntity

依赖项、软件包和类名的更改

在 v1 中,S3 事件通知 API 类会随着 S3 模块(artifactId aws-java-sdk-s3)一起以传递依赖方式导入。但在 v2 中,您需要在 s3-event-notifications 构件上添加依赖项。

更改 v1 v2

Maven 依赖项

<dependencyManagement> <dependencies> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-bom</artifactId> <version>1.X.X</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> </dependency> </dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>bom</artifactId> <version>2.X.X1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3-event-notifications</artifactId> </dependency> </dependencies>
软件包名称 com.amazonaws.services.s3.event software.amazon.awssdk.eventnotifications.s3.model
类名

S3EventNotification

S3EventNotification.S3EventNotificationRecord

S3EventNotification.GlacierEventDataEntity

S3EventNotification.IntelligentTieringEventDataEntity

S3EventNotification.LifecycleEventDataEntity

S3EventNotification.ReplicationEventDataEntity

S3EventNotification.RequestParametersEntity

S3EventNotification.ResponseElementsEntity

S3EventNotification.RestoreEventDataEntity

S3EventNotification.S3BucketEntity

S3EventNotification.S3Entity

S3EventNotification.S3ObjectEntity

S3EventNotification.TransitionEventDataEntity

S3EventNotification.UserIdentityEntity

S3EventNotification

S3EventNotificationRecord

GlacierEventData

IntelligentTieringEventData

LifecycleEventData

ReplicationEventData

RequestParameters

ResponseElements

RestoreEventData

S3Bucket

S3

S3Object

TransitionEventData

UserIdentity

1 最新版本

API 更改

JSON 和 S3EventNotification 的相互转换

应用场景 v1 v2
从 JSON 字符串创建 S3EventNotification
S3EventNotification notification = S3EventNotification.parseJson(message.body());
S3EventNotification notification = S3EventNotification.fromJson(message.body());
S3EventNotification 转换为 JSON 字符串
String json = notification.toJson();
String json = notification.toJson();

S3EventNotification 的访问属性

应用场景 v1 v2
从通知中检索记录
List<S3EventNotification.S3EventNotificationRecord> records = notifcation.getRecords();
List<S3EventNotificationRecord> records = notification.getRecords();
从记录列表中检索记录
S3EventNotification.S3EventNotificationRecord record = records.stream().findAny().get();
S3EventNotificationRecord record = records.stream().findAny().get();
检索 Glacier 事件数据
S3EventNotification.GlacierEventDataEntity glacierEventData = record.getGlacierEventData();
GlacierEventData glacierEventData = record.getGlacierEventData();
从 Glacier 事件中检索还原事件数据
S3EventNotification.RestoreEventDataEntity restoreEventData = glacierEventData.getRestoreEventDataEntity();
RestoreEventData restoreEventData = glacierEventData.getRestoreEventData();
检索请求参数
S3EventNotification.RequestParametersEntity requestParameters = record.getRequestParameters();
RequestParameters requestParameters = record.getRequestParameters();
检索智能分层事件数据
S3EventNotification.IntelligentTieringEventDataEntity tieringEventData = record.getIntelligentTieringEventData();
IntelligentTieringEventData intelligentTieringEventData = record.getIntelligentTieringEventData();
检索生命周期事件数据
S3EventNotification.LifecycleEventDataEntity lifecycleEventData = record.getLifecycleEventData();
LifecycleEventData lifecycleEventData = record.getLifecycleEventData();
以枚举形式检索事件名称
S3Event eventNameAsEnum = record.getEventNameAsEnum();
//getEventNameAsEnum does not exist; use 'getEventName()' String eventName = record.getEventName();
检索复制事件数据
S3EventNotification.ReplicationEventDataEntity replicationEntity = record.getReplicationEventDataEntity();
ReplicationEventData replicationEventData = record.getReplicationEventData();
检索 S3 存储桶和对象信息
S3EventNotification.S3Entity s3 = record.getS3();
S3 s3 = record.getS3();
检索用户身份信息
S3EventNotification.UserIdentityEntity userIdentity = record.getUserIdentity();
UserIdentity userIdentity = record.getUserIdentity();
检索响应元素
S3EventNotification.ResponseElementsEntity responseElements = record.getResponseElements();
ResponseElements responseElements = record.getResponseElements();

迁移 aws-lambda-java-events 库版本

如果您使用 aws-lambda-java-events 在 Lambda 函数中处理 S3 通知事件,我们建议您升级到最新的 3.x.x 版本。最新版本从 S3 事件通知 API 中移除了适用于 Java 的 AWS SDK 1.x 上的所有依赖项。

若要详细了解 aws-lambda-java-events 库和适用于 Java 的 SDK 2.x 在处理 S3 事件通知方面的差异,请参阅使用 Java 库(AWS SDK for Java 2.x 和 aws-lambda-java-events)在 Lambda 中处理 S3 事件