peterxcli commented on code in PR #10318: URL: https://github.com/apache/ozone/pull/10318#discussion_r3275746884
########## hadoop-ozone/ozone-manager-plugins/src/main/java/org/apache/hadoop/ozone/om/eventlistener/s3/S3EventNotification.java: ########## @@ -0,0 +1,671 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.eventlistener.s3; + +/* copy of + * software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification + * class taken from AWS SDK (1.x) with minor changes for build issues + * and removed usage of unnecessary AWS specific extension entiies: Review Comment: Is it possible that we just import classes from their sdk? ########## hadoop-ozone/ozone-manager-plugins/src/main/java/org/apache/hadoop/ozone/om/eventlistener/s3/S3EventNotificationBuilder.java: ########## @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.eventlistener.s3; + +import java.time.Instant; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.apache.hadoop.ozone.om.eventlistener.s3.S3EventNotification.S3BucketEntity; +import org.apache.hadoop.ozone.om.eventlistener.s3.S3EventNotification.S3EventNotificationRecord; +import org.apache.hadoop.ozone.om.eventlistener.s3.S3EventNotification.S3ObjectEntity; +import org.apache.hadoop.ozone.om.eventlistener.s3.S3EventNotification.UserIdentityEntity; + +/** + * This is a builder for the AWS event notification class + * com.amazonaws.services.s3.event.S3EventNotification which is part of + * AWS SDK 1.x + * + * NOTE: the above class is designed for deserialization which is why it Review Comment: ```suggestion * NOTE: the above class is designed for serialization which is why it ``` IIUC, this would be serialized to json string? ########## hadoop-ozone/ozone-manager-plugins/src/main/java/org/apache/hadoop/ozone/om/eventlistener/s3/S3EventNotification.java: ########## @@ -0,0 +1,671 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.eventlistener.s3; + +/* copy of + * software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification + * class taken from AWS SDK (1.x) with minor changes for build issues + * and removed usage of unnecessary AWS specific extension entiies: + * + * - GlacierEventDataEntity + * - LifecycleEventDataEntity + * - IntelligentTieringEventDataEntity + * - ReplicationEventDataEntity + * + * NOTE: We may not need to fork this class if we can use the SDK one directly + * but conversely we may want to make our own customizations. + * + * Original copyright below: + */ + +/* + * Copyright 2014-2025 Amazon Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://aws.amazon.com/apache2.0 + * + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES + * OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and + * limitations under the License. + */ + +//import com.amazonaws.internal.DateTimeJsonSerializer; +//import com.amazonaws.util.json.Jackson; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import java.time.OffsetDateTime; +import java.util.List; +import java.util.Map; + +/** +* A helper class that represents a strongly typed S3 EventNotification item sent +* to SQS, SNS, or Lambda. + * + * <p> + * <b>Migrating to the AWS SDK for Java v2</b> + * <p> + * The v2 equivalent of this class is + * <a href="https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.html">S3EventNotification</a> + * + * <p> + * See <a href="https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/migration.html">Migration Guide</a> + * for more information. +*/ +@SuppressWarnings("checkstyle:all") +public class S3EventNotification { + + private final List<S3EventNotificationRecord> records; + + /** + * Keys for Ozone-specific extensions in the S3 Event schema. + */ + public enum OzoneEventDataKey { + IS_DIRECTORY("isDirectory"), + IS_RECURSIVE("isRecursive"), + IS_OVERWRITE("isOverwrite"), + RENAME_FROM_KEY("renameFromKey"); + + private final String jsonKey; + + OzoneEventDataKey(String jsonKey) { + this.jsonKey = jsonKey; + } + + @Override + public String toString() { + return jsonKey; + } + } + + @JsonCreator + public S3EventNotification( + @JsonProperty(value = "Records") List<S3EventNotificationRecord> records) + { + this.records = records; + } + + /** + * <p> + * Parse the JSON string into a S3EventNotification object. + * </p> + * <p> + * The function will try its best to parse input JSON string as best as it can. + * It will not fail even if the JSON string contains unknown properties. + * The function will throw SdkClientException if the input JSON string is + * not valid JSON. + * </p> + * @param json + * JSON string to parse. Typically this is the body of your SQS + * notification message body. + * + * @return The resulting S3EventNotification object. + */ + //public static S3EventNotification parseJson(String json) { + // return Jackson.fromJsonString(json, S3EventNotification.class); + //} + + /** + * @return the records in this notification + */ + @JsonProperty(value = "Records") + public List<S3EventNotificationRecord> getRecords() { + return records; + } + + //public String toJson() { + // return Jackson.toJsonString(this); + //} + + public static class UserIdentityEntity { + + private final String principalId; + + @JsonCreator + public UserIdentityEntity( + @JsonProperty(value = "principalId") String principalId) { + this.principalId = principalId; + } + + public String getPrincipalId() { + return principalId; + } + } + + public static class S3BucketEntity { + + private final String name; + private final UserIdentityEntity ownerIdentity; + private final String arn; + + @JsonCreator + public S3BucketEntity( + @JsonProperty(value = "name") String name, + @JsonProperty(value = "ownerIdentity") UserIdentityEntity ownerIdentity, + @JsonProperty(value = "arn") String arn) + { + this.name = name; + this.ownerIdentity = ownerIdentity; + this.arn = arn; + } + + public String getName() { + return name; + } + + public UserIdentityEntity getOwnerIdentity() { + return ownerIdentity; + } + + public String getArn() { + return arn; + } + } + + public static class S3ObjectEntity { + + private final String key; + private final Long size; + private final String eTag; + private final String versionId; + private final String sequencer; + + @Deprecated + public S3ObjectEntity( + String key, + Integer size, + String eTag, + String versionId) + { + this.key = key; + this.size = size == null ? null : size.longValue(); + this.eTag = eTag; + this.versionId = versionId; + this.sequencer = null; + } + + @Deprecated + public S3ObjectEntity( + String key, + Long size, + String eTag, + String versionId) + { + this(key, size, eTag, versionId, null); + } + + @JsonCreator + public S3ObjectEntity( + @JsonProperty(value = "key") String key, + @JsonProperty(value = "size") Long size, + @JsonProperty(value = "eTag") String eTag, + @JsonProperty(value = "versionId") String versionId, + @JsonProperty(value = "sequencer") String sequencer) + { + this.key = key; + this.size = size; + this.eTag = eTag; + this.versionId = versionId; + this.sequencer = sequencer; + } + + public String getKey() { + return key; + } + + /** + * S3 URL encodes the key of the object involved in the event. This is + * a convenience method to automatically URL decode the key. + * @return The URL decoded object key. + */ + //public String getUrlDecodedKey() { + // return SdkHttpUtils.urlDecode(getKey()); + //} + + /** + * @deprecated use {@link #getSizeAsLong()} instead. + */ + @Deprecated + @JsonIgnore + public Integer getSize() { + return size == null ? null : size.intValue(); + } + + @JsonProperty(value = "size") + public Long getSizeAsLong() { + return size; + } + + public String geteTag() { + return eTag; + } + + public String getVersionId() { + return versionId; + } + + public String getSequencer() { + return sequencer; + } + } + + public static class S3Entity { + + private final String configurationId; + private final S3BucketEntity bucket; + private final S3ObjectEntity object; + private final String s3SchemaVersion; + + @JsonCreator + public S3Entity( + @JsonProperty(value = "configurationId") String configurationId, + @JsonProperty(value = "bucket") S3BucketEntity bucket, + @JsonProperty(value = "object") S3ObjectEntity object, + @JsonProperty(value = "s3SchemaVersion") String s3SchemaVersion) + { + this.configurationId = configurationId; + this.bucket = bucket; + this.object = object; + this.s3SchemaVersion = s3SchemaVersion; + } + + public String getConfigurationId() { + return configurationId; + } + + public S3BucketEntity getBucket() { + return bucket; + } + + public S3ObjectEntity getObject() { + return object; + } + + public String getS3SchemaVersion() { + return s3SchemaVersion; + } + } + + public static class RequestParametersEntity { + + private final String sourceIPAddress; + + @JsonCreator + public RequestParametersEntity( + @JsonProperty(value = "sourceIPAddress") String sourceIPAddress) + { + this.sourceIPAddress = sourceIPAddress; + } + + public String getSourceIPAddress() { + return sourceIPAddress; + } + } + + public static class ResponseElementsEntity { + + private final String xAmzId2; + private final String xAmzRequestId; + + @JsonCreator + public ResponseElementsEntity( + @JsonProperty(value = "x-amz-id-2") String xAmzId2, + @JsonProperty(value = "x-amz-request-id") String xAmzRequestId) + { + this.xAmzId2 = xAmzId2; + this.xAmzRequestId = xAmzRequestId; + } + + @JsonProperty("x-amz-id-2") + public String getxAmzId2() { + return xAmzId2; + } + + @JsonProperty("x-amz-request-id") + public String getxAmzRequestId() { + return xAmzRequestId; + } + } + + public static class GlacierEventDataEntity { + private final RestoreEventDataEntity restoreEventData; + + @JsonCreator + public GlacierEventDataEntity( + @JsonProperty(value = "restoreEventData") RestoreEventDataEntity restoreEventData) + { + this.restoreEventData = restoreEventData; + } + + public RestoreEventDataEntity getRestoreEventData() { + return restoreEventData; + } + } + + public static class LifecycleEventDataEntity { Review Comment: maybe we could keep this as lifecycle work is going to merge into main. ########## hadoop-ozone/ozone-manager-plugins/src/main/java/org/apache/hadoop/ozone/om/eventlistener/s3/S3EventNotification.java: ########## @@ -0,0 +1,671 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.eventlistener.s3; + +/* copy of + * software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification + * class taken from AWS SDK (1.x) with minor changes for build issues + * and removed usage of unnecessary AWS specific extension entiies: + * + * - GlacierEventDataEntity + * - LifecycleEventDataEntity + * - IntelligentTieringEventDataEntity + * - ReplicationEventDataEntity + * + * NOTE: We may not need to fork this class if we can use the SDK one directly + * but conversely we may want to make our own customizations. + * + * Original copyright below: + */ + +/* + * Copyright 2014-2025 Amazon Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://aws.amazon.com/apache2.0 + * + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES + * OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and + * limitations under the License. + */ + +//import com.amazonaws.internal.DateTimeJsonSerializer; +//import com.amazonaws.util.json.Jackson; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import java.time.OffsetDateTime; +import java.util.List; +import java.util.Map; + +/** +* A helper class that represents a strongly typed S3 EventNotification item sent +* to SQS, SNS, or Lambda. + * + * <p> + * <b>Migrating to the AWS SDK for Java v2</b> + * <p> + * The v2 equivalent of this class is + * <a href="https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.html">S3EventNotification</a> + * + * <p> + * See <a href="https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/migration.html">Migration Guide</a> + * for more information. +*/ +@SuppressWarnings("checkstyle:all") +public class S3EventNotification { + + private final List<S3EventNotificationRecord> records; + + /** + * Keys for Ozone-specific extensions in the S3 Event schema. + */ + public enum OzoneEventDataKey { + IS_DIRECTORY("isDirectory"), + IS_RECURSIVE("isRecursive"), + IS_OVERWRITE("isOverwrite"), + RENAME_FROM_KEY("renameFromKey"); + + private final String jsonKey; + + OzoneEventDataKey(String jsonKey) { + this.jsonKey = jsonKey; + } + + @Override + public String toString() { + return jsonKey; + } + } + + @JsonCreator + public S3EventNotification( + @JsonProperty(value = "Records") List<S3EventNotificationRecord> records) + { + this.records = records; + } + + /** + * <p> + * Parse the JSON string into a S3EventNotification object. + * </p> + * <p> + * The function will try its best to parse input JSON string as best as it can. + * It will not fail even if the JSON string contains unknown properties. + * The function will throw SdkClientException if the input JSON string is + * not valid JSON. + * </p> + * @param json + * JSON string to parse. Typically this is the body of your SQS + * notification message body. + * + * @return The resulting S3EventNotification object. + */ + //public static S3EventNotification parseJson(String json) { + // return Jackson.fromJsonString(json, S3EventNotification.class); + //} + + /** + * @return the records in this notification + */ + @JsonProperty(value = "Records") + public List<S3EventNotificationRecord> getRecords() { + return records; + } + + //public String toJson() { + // return Jackson.toJsonString(this); + //} + + public static class UserIdentityEntity { + + private final String principalId; + + @JsonCreator + public UserIdentityEntity( + @JsonProperty(value = "principalId") String principalId) { + this.principalId = principalId; + } + + public String getPrincipalId() { + return principalId; + } + } + + public static class S3BucketEntity { + + private final String name; + private final UserIdentityEntity ownerIdentity; + private final String arn; + + @JsonCreator + public S3BucketEntity( + @JsonProperty(value = "name") String name, + @JsonProperty(value = "ownerIdentity") UserIdentityEntity ownerIdentity, + @JsonProperty(value = "arn") String arn) + { + this.name = name; + this.ownerIdentity = ownerIdentity; + this.arn = arn; + } + + public String getName() { + return name; + } + + public UserIdentityEntity getOwnerIdentity() { + return ownerIdentity; + } + + public String getArn() { + return arn; + } + } + + public static class S3ObjectEntity { + + private final String key; + private final Long size; + private final String eTag; + private final String versionId; + private final String sequencer; + + @Deprecated + public S3ObjectEntity( + String key, + Integer size, + String eTag, + String versionId) + { + this.key = key; + this.size = size == null ? null : size.longValue(); + this.eTag = eTag; + this.versionId = versionId; + this.sequencer = null; + } + + @Deprecated + public S3ObjectEntity( + String key, + Long size, + String eTag, + String versionId) + { + this(key, size, eTag, versionId, null); + } + + @JsonCreator + public S3ObjectEntity( + @JsonProperty(value = "key") String key, + @JsonProperty(value = "size") Long size, + @JsonProperty(value = "eTag") String eTag, + @JsonProperty(value = "versionId") String versionId, + @JsonProperty(value = "sequencer") String sequencer) + { + this.key = key; + this.size = size; + this.eTag = eTag; + this.versionId = versionId; + this.sequencer = sequencer; + } + + public String getKey() { + return key; + } + + /** + * S3 URL encodes the key of the object involved in the event. This is + * a convenience method to automatically URL decode the key. + * @return The URL decoded object key. + */ + //public String getUrlDecodedKey() { + // return SdkHttpUtils.urlDecode(getKey()); + //} + + /** + * @deprecated use {@link #getSizeAsLong()} instead. + */ + @Deprecated + @JsonIgnore + public Integer getSize() { + return size == null ? null : size.intValue(); + } + + @JsonProperty(value = "size") + public Long getSizeAsLong() { + return size; + } + + public String geteTag() { + return eTag; + } + + public String getVersionId() { + return versionId; + } + + public String getSequencer() { + return sequencer; + } + } + + public static class S3Entity { + + private final String configurationId; + private final S3BucketEntity bucket; + private final S3ObjectEntity object; + private final String s3SchemaVersion; + + @JsonCreator + public S3Entity( + @JsonProperty(value = "configurationId") String configurationId, + @JsonProperty(value = "bucket") S3BucketEntity bucket, + @JsonProperty(value = "object") S3ObjectEntity object, + @JsonProperty(value = "s3SchemaVersion") String s3SchemaVersion) + { + this.configurationId = configurationId; + this.bucket = bucket; + this.object = object; + this.s3SchemaVersion = s3SchemaVersion; + } + + public String getConfigurationId() { + return configurationId; + } + + public S3BucketEntity getBucket() { + return bucket; + } + + public S3ObjectEntity getObject() { + return object; + } + + public String getS3SchemaVersion() { + return s3SchemaVersion; + } + } + + public static class RequestParametersEntity { + + private final String sourceIPAddress; + + @JsonCreator + public RequestParametersEntity( + @JsonProperty(value = "sourceIPAddress") String sourceIPAddress) + { + this.sourceIPAddress = sourceIPAddress; + } + + public String getSourceIPAddress() { + return sourceIPAddress; + } + } + + public static class ResponseElementsEntity { + + private final String xAmzId2; + private final String xAmzRequestId; + + @JsonCreator + public ResponseElementsEntity( + @JsonProperty(value = "x-amz-id-2") String xAmzId2, + @JsonProperty(value = "x-amz-request-id") String xAmzRequestId) + { + this.xAmzId2 = xAmzId2; + this.xAmzRequestId = xAmzRequestId; + } + + @JsonProperty("x-amz-id-2") + public String getxAmzId2() { + return xAmzId2; + } + + @JsonProperty("x-amz-request-id") + public String getxAmzRequestId() { + return xAmzRequestId; + } + } + + public static class GlacierEventDataEntity { Review Comment: > * and removed usage of unnecessary AWS specific extension entiies: > * - GlacierEventDataEntity > * - LifecycleEventDataEntity > * - IntelligentTieringEventDataEntity > * - ReplicationEventDataEntity remove? ########## hadoop-ozone/ozone-manager-plugins/src/main/java/org/apache/hadoop/ozone/om/eventlistener/s3/S3EventNotificationStrategy.java: ########## @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.eventlistener.s3; + +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.time.Instant; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.ozone.om.eventlistener.OMEventListenerNotificationStrategy; +import org.apache.hadoop.ozone.om.eventlistener.s3.S3EventNotification.OzoneEventDataKey; +import org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is a notification strategy to generate events according to S3 + * notification semantics. + */ +public class S3EventNotificationStrategy implements OMEventListenerNotificationStrategy { + public static final Logger LOG = LoggerFactory.getLogger(S3EventNotificationStrategy.class); + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @Override + public List<String> determineEventsForOperation(OmCompletedRequestInfo requestInfo) { + + switch (requestInfo.getCmdType()) { + case CreateKey: + case CommitKey: + return Collections.singletonList(createS3Event("ObjectCreated:Put", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + Collections.emptyMap()) + ); + case CreateFile: + OmCompletedRequestInfo.OperationArgs.CreateFileArgs createFileArgs + = (OmCompletedRequestInfo.OperationArgs.CreateFileArgs) requestInfo.getOpArgs(); + + // XXX: ozoneEventData is an Ozone extension. Its is unclear if this + // schema makes sense but the general S3 schema is somewhat + // freeform. These arguments are more informational than + // required so it is unclear as to their necessity. + Map<String, Object> renameEventData = new HashMap<>(); + renameEventData.put(OzoneEventDataKey.IS_DIRECTORY.toString(), false); + renameEventData.put(OzoneEventDataKey.IS_RECURSIVE.toString(), createFileArgs.isRecursive()); + renameEventData.put(OzoneEventDataKey.IS_OVERWRITE.toString(), createFileArgs.isOverwrite()); + + return Collections.singletonList(createS3Event("ObjectCreated:Put", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + renameEventData) + ); + case CreateDirectory: + // XXX: ozoneEventData is an Ozone extension. Its is unclear if this + // schema makes sense but the general S3 schema is somewhat + // freeform. These arguments are more informational than + // required so it is unclear as to their necessity. + Map<String, Object> createEventData = new HashMap<>(); + createEventData.put(OzoneEventDataKey.IS_DIRECTORY.toString(), true); + + return Collections.singletonList(createS3Event("ObjectCreated:Put", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + createEventData) + ); + case DeleteKey: + return Collections.singletonList(createS3Event("ObjectRemoved:Delete", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + Collections.emptyMap()) + ); + case RenameKey: + OmCompletedRequestInfo.OperationArgs.RenameKeyArgs renameKeyArgs + = (OmCompletedRequestInfo.OperationArgs.RenameKeyArgs) requestInfo.getOpArgs(); + + String renameFromKey = S3OzoneEventKeyFormatter.getOzoneKey(requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName()); + + // XXX: it would be good to be able to convey that this was a + // file vs directory rename + Map<String, Object> ozoneEventData = new HashMap<>(); + ozoneEventData.put(OzoneEventDataKey.RENAME_FROM_KEY.toString(), renameFromKey); + + // NOTE: ObjectRenamed:Rename is an Ozone extension as is the + // ozoneEventData map in the S3 event schema. + return Collections.singletonList(createS3Event("ObjectRenamed:Rename", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + renameKeyArgs.getToKeyName(), + ozoneEventData) + ); + default: + LOG.info("No events for operation {} on {}", + requestInfo.getCmdType(), + requestInfo.getKeyName()); + return Collections.emptyList(); + } + } + + static String createS3Event(String eventName, + String volumeName, + String bucketName, + String keyName, + Map<String, Object> ozoneEventData) { + try { + String objectKey = S3OzoneEventKeyFormatter.getOzoneKey(volumeName, bucketName, keyName); + String bucketArn = "arn:aws:s3:::" + volumeName + "." + bucketName; + Instant eventTime = Instant.now(); + String etag = UUID.randomUUID().toString(); + + S3EventNotification event = new S3EventNotificationBuilder(objectKey, bucketName, bucketArn, + eventName, eventTime, etag) + .addAllEventData(ozoneEventData) + .build(); + + return MAPPER.writer().writeValueAsString(event); + } catch (Exception ex) { + LOG.info("------------> {}", "failed"); + return null; + } + } + + /** + * Formats the Ozone key for S3 events. + * + * NOTE: This differs from OMMetadataManager#getOzoneKey in that it does NOT + * include the leading slash, which is standard for S3 notification keys. + */ + private static class S3OzoneEventKeyFormatter { + public static String getOzoneKey(String volume, String bucket, String key) { + StringBuilder builder = new StringBuilder(); + builder.append(volume); + // TODO : Throw if the Bucket is null? Review Comment: https://github.com/apache/ozone/pull/9364 looks like only volume create/delete dont have bucket in the proto, but we also dont handle the volume related operation here. ########## hadoop-ozone/ozone-manager-plugins/src/main/java/org/apache/hadoop/ozone/om/eventlistener/s3/S3EventNotificationStrategy.java: ########## @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.eventlistener.s3; + +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.time.Instant; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.ozone.om.eventlistener.OMEventListenerNotificationStrategy; +import org.apache.hadoop.ozone.om.eventlistener.s3.S3EventNotification.OzoneEventDataKey; +import org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is a notification strategy to generate events according to S3 + * notification semantics. + */ +public class S3EventNotificationStrategy implements OMEventListenerNotificationStrategy { + public static final Logger LOG = LoggerFactory.getLogger(S3EventNotificationStrategy.class); + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @Override + public List<String> determineEventsForOperation(OmCompletedRequestInfo requestInfo) { + + switch (requestInfo.getCmdType()) { + case CreateKey: + case CommitKey: + return Collections.singletonList(createS3Event("ObjectCreated:Put", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + Collections.emptyMap()) + ); + case CreateFile: + OmCompletedRequestInfo.OperationArgs.CreateFileArgs createFileArgs + = (OmCompletedRequestInfo.OperationArgs.CreateFileArgs) requestInfo.getOpArgs(); + + // XXX: ozoneEventData is an Ozone extension. Its is unclear if this + // schema makes sense but the general S3 schema is somewhat + // freeform. These arguments are more informational than + // required so it is unclear as to their necessity. + Map<String, Object> renameEventData = new HashMap<>(); + renameEventData.put(OzoneEventDataKey.IS_DIRECTORY.toString(), false); + renameEventData.put(OzoneEventDataKey.IS_RECURSIVE.toString(), createFileArgs.isRecursive()); + renameEventData.put(OzoneEventDataKey.IS_OVERWRITE.toString(), createFileArgs.isOverwrite()); + + return Collections.singletonList(createS3Event("ObjectCreated:Put", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + renameEventData) + ); + case CreateDirectory: + // XXX: ozoneEventData is an Ozone extension. Its is unclear if this + // schema makes sense but the general S3 schema is somewhat + // freeform. These arguments are more informational than + // required so it is unclear as to their necessity. + Map<String, Object> createEventData = new HashMap<>(); + createEventData.put(OzoneEventDataKey.IS_DIRECTORY.toString(), true); + + return Collections.singletonList(createS3Event("ObjectCreated:Put", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + createEventData) + ); + case DeleteKey: + return Collections.singletonList(createS3Event("ObjectRemoved:Delete", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + Collections.emptyMap()) + ); + case RenameKey: + OmCompletedRequestInfo.OperationArgs.RenameKeyArgs renameKeyArgs + = (OmCompletedRequestInfo.OperationArgs.RenameKeyArgs) requestInfo.getOpArgs(); + + String renameFromKey = S3OzoneEventKeyFormatter.getOzoneKey(requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName()); + + // XXX: it would be good to be able to convey that this was a + // file vs directory rename + Map<String, Object> ozoneEventData = new HashMap<>(); + ozoneEventData.put(OzoneEventDataKey.RENAME_FROM_KEY.toString(), renameFromKey); + + // NOTE: ObjectRenamed:Rename is an Ozone extension as is the + // ozoneEventData map in the S3 event schema. + return Collections.singletonList(createS3Event("ObjectRenamed:Rename", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + renameKeyArgs.getToKeyName(), + ozoneEventData) + ); + default: + LOG.info("No events for operation {} on {}", + requestInfo.getCmdType(), + requestInfo.getKeyName()); + return Collections.emptyList(); + } + } + + static String createS3Event(String eventName, + String volumeName, + String bucketName, + String keyName, + Map<String, Object> ozoneEventData) { + try { + String objectKey = S3OzoneEventKeyFormatter.getOzoneKey(volumeName, bucketName, keyName); + String bucketArn = "arn:aws:s3:::" + volumeName + "." + bucketName; + Instant eventTime = Instant.now(); + String etag = UUID.randomUUID().toString(); + + S3EventNotification event = new S3EventNotificationBuilder(objectKey, bucketName, bucketArn, + eventName, eventTime, etag) + .addAllEventData(ozoneEventData) + .build(); + + return MAPPER.writer().writeValueAsString(event); + } catch (Exception ex) { + LOG.info("------------> {}", "failed"); + return null; + } + } + + /** + * Formats the Ozone key for S3 events. + * + * NOTE: This differs from OMMetadataManager#getOzoneKey in that it does NOT + * include the leading slash, which is standard for S3 notification keys. + */ + private static class S3OzoneEventKeyFormatter { + public static String getOzoneKey(String volume, String bucket, String key) { + StringBuilder builder = new StringBuilder(); + builder.append(volume); + // TODO : Throw if the Bucket is null? Review Comment: > Throw if the Bucket is null? Is this possible? ########## hadoop-ozone/ozone-manager-plugins/src/main/java/org/apache/hadoop/ozone/om/eventlistener/s3/S3EventNotificationStrategy.java: ########## @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.eventlistener.s3; + +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.time.Instant; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.ozone.om.eventlistener.OMEventListenerNotificationStrategy; +import org.apache.hadoop.ozone.om.eventlistener.s3.S3EventNotification.OzoneEventDataKey; +import org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is a notification strategy to generate events according to S3 + * notification semantics. + */ +public class S3EventNotificationStrategy implements OMEventListenerNotificationStrategy { + public static final Logger LOG = LoggerFactory.getLogger(S3EventNotificationStrategy.class); + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @Override + public List<String> determineEventsForOperation(OmCompletedRequestInfo requestInfo) { + + switch (requestInfo.getCmdType()) { + case CreateKey: + case CommitKey: + return Collections.singletonList(createS3Event("ObjectCreated:Put", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + Collections.emptyMap()) + ); + case CreateFile: + OmCompletedRequestInfo.OperationArgs.CreateFileArgs createFileArgs + = (OmCompletedRequestInfo.OperationArgs.CreateFileArgs) requestInfo.getOpArgs(); + + // XXX: ozoneEventData is an Ozone extension. Its is unclear if this + // schema makes sense but the general S3 schema is somewhat + // freeform. These arguments are more informational than + // required so it is unclear as to their necessity. + Map<String, Object> renameEventData = new HashMap<>(); + renameEventData.put(OzoneEventDataKey.IS_DIRECTORY.toString(), false); + renameEventData.put(OzoneEventDataKey.IS_RECURSIVE.toString(), createFileArgs.isRecursive()); + renameEventData.put(OzoneEventDataKey.IS_OVERWRITE.toString(), createFileArgs.isOverwrite()); + + return Collections.singletonList(createS3Event("ObjectCreated:Put", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + renameEventData) + ); + case CreateDirectory: + // XXX: ozoneEventData is an Ozone extension. Its is unclear if this + // schema makes sense but the general S3 schema is somewhat + // freeform. These arguments are more informational than + // required so it is unclear as to their necessity. + Map<String, Object> createEventData = new HashMap<>(); + createEventData.put(OzoneEventDataKey.IS_DIRECTORY.toString(), true); + + return Collections.singletonList(createS3Event("ObjectCreated:Put", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + createEventData) + ); + case DeleteKey: + return Collections.singletonList(createS3Event("ObjectRemoved:Delete", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + Collections.emptyMap()) + ); + case RenameKey: + OmCompletedRequestInfo.OperationArgs.RenameKeyArgs renameKeyArgs + = (OmCompletedRequestInfo.OperationArgs.RenameKeyArgs) requestInfo.getOpArgs(); + + String renameFromKey = S3OzoneEventKeyFormatter.getOzoneKey(requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName()); + + // XXX: it would be good to be able to convey that this was a + // file vs directory rename + Map<String, Object> ozoneEventData = new HashMap<>(); + ozoneEventData.put(OzoneEventDataKey.RENAME_FROM_KEY.toString(), renameFromKey); + + // NOTE: ObjectRenamed:Rename is an Ozone extension as is the + // ozoneEventData map in the S3 event schema. + return Collections.singletonList(createS3Event("ObjectRenamed:Rename", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + renameKeyArgs.getToKeyName(), + ozoneEventData) + ); + default: + LOG.info("No events for operation {} on {}", Review Comment: ```suggestion LOG.debug("No events for operation {} on {}", ``` ########## hadoop-ozone/ozone-manager-plugins/src/main/java/org/apache/hadoop/ozone/om/eventlistener/s3/S3EventNotificationStrategy.java: ########## @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.eventlistener.s3; + +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.time.Instant; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.ozone.om.eventlistener.OMEventListenerNotificationStrategy; +import org.apache.hadoop.ozone.om.eventlistener.s3.S3EventNotification.OzoneEventDataKey; +import org.apache.hadoop.ozone.om.helpers.OmCompletedRequestInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is a notification strategy to generate events according to S3 + * notification semantics. + */ +public class S3EventNotificationStrategy implements OMEventListenerNotificationStrategy { + public static final Logger LOG = LoggerFactory.getLogger(S3EventNotificationStrategy.class); + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @Override + public List<String> determineEventsForOperation(OmCompletedRequestInfo requestInfo) { + + switch (requestInfo.getCmdType()) { + case CreateKey: + case CommitKey: + return Collections.singletonList(createS3Event("ObjectCreated:Put", + requestInfo.getVolumeName(), + requestInfo.getBucketName(), + requestInfo.getKeyName(), + Collections.emptyMap()) + ); + case CreateFile: + OmCompletedRequestInfo.OperationArgs.CreateFileArgs createFileArgs + = (OmCompletedRequestInfo.OperationArgs.CreateFileArgs) requestInfo.getOpArgs(); + + // XXX: ozoneEventData is an Ozone extension. Its is unclear if this + // schema makes sense but the general S3 schema is somewhat + // freeform. These arguments are more informational than + // required so it is unclear as to their necessity. + Map<String, Object> renameEventData = new HashMap<>(); + renameEventData.put(OzoneEventDataKey.IS_DIRECTORY.toString(), false); + renameEventData.put(OzoneEventDataKey.IS_RECURSIVE.toString(), createFileArgs.isRecursive()); + renameEventData.put(OzoneEventDataKey.IS_OVERWRITE.toString(), createFileArgs.isOverwrite()); Review Comment: typo? ```suggestion createEventData.put(OzoneEventDataKey.IS_DIRECTORY.toString(), false); createEventData.put(OzoneEventDataKey.IS_RECURSIVE.toString(), createFileArgs.isRecursive()); createEventData.put(OzoneEventDataKey.IS_OVERWRITE.toString(), createFileArgs.isOverwrite()); ``` ########## hadoop-ozone/ozone-manager-plugins/src/main/java/org/apache/hadoop/ozone/om/eventlistener/s3/S3EventNotification.java: ########## @@ -0,0 +1,671 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.eventlistener.s3; + +/* copy of + * software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification + * class taken from AWS SDK (1.x) with minor changes for build issues + * and removed usage of unnecessary AWS specific extension entiies: + * + * - GlacierEventDataEntity + * - LifecycleEventDataEntity + * - IntelligentTieringEventDataEntity + * - ReplicationEventDataEntity + * + * NOTE: We may not need to fork this class if we can use the SDK one directly + * but conversely we may want to make our own customizations. + * + * Original copyright below: + */ + +/* + * Copyright 2014-2025 Amazon Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://aws.amazon.com/apache2.0 + * + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES + * OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and + * limitations under the License. + */ + +//import com.amazonaws.internal.DateTimeJsonSerializer; +//import com.amazonaws.util.json.Jackson; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import java.time.OffsetDateTime; +import java.util.List; +import java.util.Map; + +/** +* A helper class that represents a strongly typed S3 EventNotification item sent +* to SQS, SNS, or Lambda. + * + * <p> + * <b>Migrating to the AWS SDK for Java v2</b> + * <p> + * The v2 equivalent of this class is + * <a href="https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.html">S3EventNotification</a> + * + * <p> + * See <a href="https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/migration.html">Migration Guide</a> + * for more information. +*/ +@SuppressWarnings("checkstyle:all") +public class S3EventNotification { + + private final List<S3EventNotificationRecord> records; + + /** + * Keys for Ozone-specific extensions in the S3 Event schema. + */ + public enum OzoneEventDataKey { + IS_DIRECTORY("isDirectory"), + IS_RECURSIVE("isRecursive"), + IS_OVERWRITE("isOverwrite"), + RENAME_FROM_KEY("renameFromKey"); + + private final String jsonKey; + + OzoneEventDataKey(String jsonKey) { + this.jsonKey = jsonKey; + } + + @Override + public String toString() { + return jsonKey; + } + } + + @JsonCreator + public S3EventNotification( + @JsonProperty(value = "Records") List<S3EventNotificationRecord> records) + { + this.records = records; + } + + /** + * <p> + * Parse the JSON string into a S3EventNotification object. + * </p> + * <p> + * The function will try its best to parse input JSON string as best as it can. + * It will not fail even if the JSON string contains unknown properties. + * The function will throw SdkClientException if the input JSON string is + * not valid JSON. + * </p> + * @param json + * JSON string to parse. Typically this is the body of your SQS + * notification message body. + * + * @return The resulting S3EventNotification object. + */ + //public static S3EventNotification parseJson(String json) { + // return Jackson.fromJsonString(json, S3EventNotification.class); + //} + + /** + * @return the records in this notification + */ + @JsonProperty(value = "Records") + public List<S3EventNotificationRecord> getRecords() { + return records; + } + + //public String toJson() { + // return Jackson.toJsonString(this); + //} + + public static class UserIdentityEntity { + + private final String principalId; + + @JsonCreator + public UserIdentityEntity( + @JsonProperty(value = "principalId") String principalId) { + this.principalId = principalId; + } + + public String getPrincipalId() { + return principalId; + } + } + + public static class S3BucketEntity { + + private final String name; + private final UserIdentityEntity ownerIdentity; + private final String arn; + + @JsonCreator + public S3BucketEntity( + @JsonProperty(value = "name") String name, + @JsonProperty(value = "ownerIdentity") UserIdentityEntity ownerIdentity, + @JsonProperty(value = "arn") String arn) + { + this.name = name; + this.ownerIdentity = ownerIdentity; + this.arn = arn; + } + + public String getName() { + return name; + } + + public UserIdentityEntity getOwnerIdentity() { + return ownerIdentity; + } + + public String getArn() { + return arn; + } + } + + public static class S3ObjectEntity { + + private final String key; + private final Long size; + private final String eTag; + private final String versionId; + private final String sequencer; + + @Deprecated + public S3ObjectEntity( + String key, + Integer size, + String eTag, + String versionId) + { + this.key = key; + this.size = size == null ? null : size.longValue(); + this.eTag = eTag; + this.versionId = versionId; + this.sequencer = null; + } + + @Deprecated + public S3ObjectEntity( + String key, + Long size, + String eTag, + String versionId) + { + this(key, size, eTag, versionId, null); + } + + @JsonCreator + public S3ObjectEntity( + @JsonProperty(value = "key") String key, + @JsonProperty(value = "size") Long size, + @JsonProperty(value = "eTag") String eTag, + @JsonProperty(value = "versionId") String versionId, + @JsonProperty(value = "sequencer") String sequencer) + { + this.key = key; + this.size = size; + this.eTag = eTag; + this.versionId = versionId; + this.sequencer = sequencer; + } + + public String getKey() { + return key; + } + + /** + * S3 URL encodes the key of the object involved in the event. This is + * a convenience method to automatically URL decode the key. + * @return The URL decoded object key. + */ + //public String getUrlDecodedKey() { + // return SdkHttpUtils.urlDecode(getKey()); + //} + + /** + * @deprecated use {@link #getSizeAsLong()} instead. + */ + @Deprecated + @JsonIgnore + public Integer getSize() { + return size == null ? null : size.intValue(); + } + + @JsonProperty(value = "size") + public Long getSizeAsLong() { + return size; + } + + public String geteTag() { + return eTag; + } + + public String getVersionId() { + return versionId; + } + + public String getSequencer() { + return sequencer; + } + } + + public static class S3Entity { + + private final String configurationId; + private final S3BucketEntity bucket; + private final S3ObjectEntity object; + private final String s3SchemaVersion; + + @JsonCreator + public S3Entity( + @JsonProperty(value = "configurationId") String configurationId, + @JsonProperty(value = "bucket") S3BucketEntity bucket, + @JsonProperty(value = "object") S3ObjectEntity object, + @JsonProperty(value = "s3SchemaVersion") String s3SchemaVersion) + { + this.configurationId = configurationId; + this.bucket = bucket; + this.object = object; + this.s3SchemaVersion = s3SchemaVersion; + } + + public String getConfigurationId() { + return configurationId; + } + + public S3BucketEntity getBucket() { + return bucket; + } + + public S3ObjectEntity getObject() { + return object; + } + + public String getS3SchemaVersion() { + return s3SchemaVersion; + } + } + + public static class RequestParametersEntity { + + private final String sourceIPAddress; + + @JsonCreator + public RequestParametersEntity( + @JsonProperty(value = "sourceIPAddress") String sourceIPAddress) + { + this.sourceIPAddress = sourceIPAddress; + } + + public String getSourceIPAddress() { + return sourceIPAddress; + } + } + + public static class ResponseElementsEntity { + + private final String xAmzId2; + private final String xAmzRequestId; + + @JsonCreator + public ResponseElementsEntity( + @JsonProperty(value = "x-amz-id-2") String xAmzId2, + @JsonProperty(value = "x-amz-request-id") String xAmzRequestId) + { + this.xAmzId2 = xAmzId2; + this.xAmzRequestId = xAmzRequestId; + } + + @JsonProperty("x-amz-id-2") + public String getxAmzId2() { + return xAmzId2; + } + + @JsonProperty("x-amz-request-id") + public String getxAmzRequestId() { + return xAmzRequestId; + } + } + + public static class GlacierEventDataEntity { + private final RestoreEventDataEntity restoreEventData; + + @JsonCreator + public GlacierEventDataEntity( + @JsonProperty(value = "restoreEventData") RestoreEventDataEntity restoreEventData) + { + this.restoreEventData = restoreEventData; + } + + public RestoreEventDataEntity getRestoreEventData() { + return restoreEventData; + } + } + + public static class LifecycleEventDataEntity { + + private final TransitionEventDataEntity transitionEventData; + + @JsonCreator + public LifecycleEventDataEntity( + @JsonProperty(value = "transitionEventData") TransitionEventDataEntity transitionEventData) + { + + this.transitionEventData = transitionEventData; + } + + public TransitionEventDataEntity getTransitionEventData() { + return transitionEventData; + } + } + + public static class IntelligentTieringEventDataEntity { + + private final String destinationAccessTier; + + @JsonCreator + public IntelligentTieringEventDataEntity( + @JsonProperty(value = "destinationAccessTier") String destinationAccessTier) + { + this.destinationAccessTier = destinationAccessTier; + } + + @JsonProperty("destinationAccessTier") + public String getDestinationAccessTier() { + return destinationAccessTier; + } + } + + public static class ReplicationEventDataEntity { Review Comment: https://github.com/apache/ozone/pull/10318/changes#r3275801889 ########## hadoop-ozone/ozone-manager-plugins/src/main/java/org/apache/hadoop/ozone/om/eventlistener/s3/S3EventNotification.java: ########## @@ -0,0 +1,671 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.eventlistener.s3; + +/* copy of + * software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification + * class taken from AWS SDK (1.x) with minor changes for build issues + * and removed usage of unnecessary AWS specific extension entiies: + * + * - GlacierEventDataEntity + * - LifecycleEventDataEntity + * - IntelligentTieringEventDataEntity + * - ReplicationEventDataEntity + * + * NOTE: We may not need to fork this class if we can use the SDK one directly + * but conversely we may want to make our own customizations. + * + * Original copyright below: + */ + +/* + * Copyright 2014-2025 Amazon Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://aws.amazon.com/apache2.0 + * + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES + * OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and + * limitations under the License. + */ + +//import com.amazonaws.internal.DateTimeJsonSerializer; +//import com.amazonaws.util.json.Jackson; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import java.time.OffsetDateTime; +import java.util.List; +import java.util.Map; + +/** +* A helper class that represents a strongly typed S3 EventNotification item sent +* to SQS, SNS, or Lambda. + * + * <p> + * <b>Migrating to the AWS SDK for Java v2</b> + * <p> + * The v2 equivalent of this class is + * <a href="https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.html">S3EventNotification</a> + * + * <p> + * See <a href="https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/migration.html">Migration Guide</a> + * for more information. +*/ +@SuppressWarnings("checkstyle:all") +public class S3EventNotification { + + private final List<S3EventNotificationRecord> records; + + /** + * Keys for Ozone-specific extensions in the S3 Event schema. + */ + public enum OzoneEventDataKey { + IS_DIRECTORY("isDirectory"), + IS_RECURSIVE("isRecursive"), + IS_OVERWRITE("isOverwrite"), + RENAME_FROM_KEY("renameFromKey"); + + private final String jsonKey; + + OzoneEventDataKey(String jsonKey) { + this.jsonKey = jsonKey; + } + + @Override + public String toString() { + return jsonKey; + } + } + + @JsonCreator + public S3EventNotification( + @JsonProperty(value = "Records") List<S3EventNotificationRecord> records) + { + this.records = records; + } + + /** + * <p> + * Parse the JSON string into a S3EventNotification object. + * </p> + * <p> + * The function will try its best to parse input JSON string as best as it can. + * It will not fail even if the JSON string contains unknown properties. + * The function will throw SdkClientException if the input JSON string is + * not valid JSON. + * </p> + * @param json + * JSON string to parse. Typically this is the body of your SQS + * notification message body. + * + * @return The resulting S3EventNotification object. + */ + //public static S3EventNotification parseJson(String json) { + // return Jackson.fromJsonString(json, S3EventNotification.class); + //} + + /** + * @return the records in this notification + */ + @JsonProperty(value = "Records") + public List<S3EventNotificationRecord> getRecords() { + return records; + } + + //public String toJson() { + // return Jackson.toJsonString(this); + //} + + public static class UserIdentityEntity { + + private final String principalId; + + @JsonCreator + public UserIdentityEntity( + @JsonProperty(value = "principalId") String principalId) { + this.principalId = principalId; + } + + public String getPrincipalId() { + return principalId; + } + } + + public static class S3BucketEntity { + + private final String name; + private final UserIdentityEntity ownerIdentity; + private final String arn; + + @JsonCreator + public S3BucketEntity( + @JsonProperty(value = "name") String name, + @JsonProperty(value = "ownerIdentity") UserIdentityEntity ownerIdentity, + @JsonProperty(value = "arn") String arn) + { + this.name = name; + this.ownerIdentity = ownerIdentity; + this.arn = arn; + } + + public String getName() { + return name; + } + + public UserIdentityEntity getOwnerIdentity() { + return ownerIdentity; + } + + public String getArn() { + return arn; + } + } + + public static class S3ObjectEntity { + + private final String key; + private final Long size; + private final String eTag; + private final String versionId; + private final String sequencer; + + @Deprecated + public S3ObjectEntity( + String key, + Integer size, + String eTag, + String versionId) + { + this.key = key; + this.size = size == null ? null : size.longValue(); + this.eTag = eTag; + this.versionId = versionId; + this.sequencer = null; + } + + @Deprecated + public S3ObjectEntity( + String key, + Long size, + String eTag, + String versionId) + { + this(key, size, eTag, versionId, null); + } + + @JsonCreator + public S3ObjectEntity( + @JsonProperty(value = "key") String key, + @JsonProperty(value = "size") Long size, + @JsonProperty(value = "eTag") String eTag, + @JsonProperty(value = "versionId") String versionId, + @JsonProperty(value = "sequencer") String sequencer) + { + this.key = key; + this.size = size; + this.eTag = eTag; + this.versionId = versionId; + this.sequencer = sequencer; + } + + public String getKey() { + return key; + } + + /** + * S3 URL encodes the key of the object involved in the event. This is + * a convenience method to automatically URL decode the key. + * @return The URL decoded object key. + */ + //public String getUrlDecodedKey() { + // return SdkHttpUtils.urlDecode(getKey()); + //} + + /** + * @deprecated use {@link #getSizeAsLong()} instead. + */ + @Deprecated + @JsonIgnore + public Integer getSize() { + return size == null ? null : size.intValue(); + } + + @JsonProperty(value = "size") + public Long getSizeAsLong() { + return size; + } + + public String geteTag() { + return eTag; + } + + public String getVersionId() { + return versionId; + } + + public String getSequencer() { + return sequencer; + } + } + + public static class S3Entity { + + private final String configurationId; + private final S3BucketEntity bucket; + private final S3ObjectEntity object; + private final String s3SchemaVersion; + + @JsonCreator + public S3Entity( + @JsonProperty(value = "configurationId") String configurationId, + @JsonProperty(value = "bucket") S3BucketEntity bucket, + @JsonProperty(value = "object") S3ObjectEntity object, + @JsonProperty(value = "s3SchemaVersion") String s3SchemaVersion) + { + this.configurationId = configurationId; + this.bucket = bucket; + this.object = object; + this.s3SchemaVersion = s3SchemaVersion; + } + + public String getConfigurationId() { + return configurationId; + } + + public S3BucketEntity getBucket() { + return bucket; + } + + public S3ObjectEntity getObject() { + return object; + } + + public String getS3SchemaVersion() { + return s3SchemaVersion; + } + } + + public static class RequestParametersEntity { + + private final String sourceIPAddress; + + @JsonCreator + public RequestParametersEntity( + @JsonProperty(value = "sourceIPAddress") String sourceIPAddress) + { + this.sourceIPAddress = sourceIPAddress; + } + + public String getSourceIPAddress() { + return sourceIPAddress; + } + } + + public static class ResponseElementsEntity { + + private final String xAmzId2; + private final String xAmzRequestId; + + @JsonCreator + public ResponseElementsEntity( + @JsonProperty(value = "x-amz-id-2") String xAmzId2, + @JsonProperty(value = "x-amz-request-id") String xAmzRequestId) + { + this.xAmzId2 = xAmzId2; + this.xAmzRequestId = xAmzRequestId; + } + + @JsonProperty("x-amz-id-2") + public String getxAmzId2() { + return xAmzId2; + } + + @JsonProperty("x-amz-request-id") + public String getxAmzRequestId() { + return xAmzRequestId; + } + } + + public static class GlacierEventDataEntity { + private final RestoreEventDataEntity restoreEventData; + + @JsonCreator + public GlacierEventDataEntity( + @JsonProperty(value = "restoreEventData") RestoreEventDataEntity restoreEventData) + { + this.restoreEventData = restoreEventData; + } + + public RestoreEventDataEntity getRestoreEventData() { + return restoreEventData; + } + } + + public static class LifecycleEventDataEntity { + + private final TransitionEventDataEntity transitionEventData; + + @JsonCreator + public LifecycleEventDataEntity( + @JsonProperty(value = "transitionEventData") TransitionEventDataEntity transitionEventData) + { + + this.transitionEventData = transitionEventData; + } + + public TransitionEventDataEntity getTransitionEventData() { + return transitionEventData; + } + } + + public static class IntelligentTieringEventDataEntity { Review Comment: https://github.com/apache/ozone/pull/10318/changes#r3275801889 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
