Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(s3): emit post events for browser uploads (#2577)

Aniket Shuklad8ef1c8d7b91
diff --git a/docs/services/s3.md b/docs/services/s3.md
index 8d9eb2aecb..c431bfd8d7 100644
--- a/docs/services/s3.md
+++ b/docs/services/s3.md
@@ -28,6 +28,11 @@
**RestoreObject** is accepted but stubbed: Floci validates the request and returns `202 Accepted`, but no restore state machine runs.
+## Event Notifications
+
+Browser and presigned POST uploads emit `s3:ObjectCreated:Post`, matching AWS S3. They do not emit
+`s3:ObjectCreated:Put`; use `s3:ObjectCreated:*` to subscribe to objects created by either method.
+
## Website Hosting
Static website requests use a bucket website hostname such as
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
index ce77522f11..0b89eb5b16 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
@@ -2698,7 +2698,7 @@ public class S3Controller {
}
}
- S3Object obj = s3Service.putObject(bucket, key, fileData, objectContentType,
+ S3Object obj = s3Service.postObject(bucket, key, fileData, objectContentType,
metadata.isEmpty() ? null : metadata);
LOG.infov("Presigned POST upload: {0}/{1} ({2} bytes)", bucket, key, fileData.length);
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java
index f65b4a6e6c..a1db068fae 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java
@@ -343,8 +343,24 @@ public class S3Service implements Resettable, ResourceProvider {
public S3Object putObject(String bucketName, String key, byte[] data,
String contentType, Map<String, String> metadata, PutObjectOptions options) {
+ return createObject(bucketName, key, data, contentType, metadata, options, "ObjectCreated:Put");
+ }
+
+ public S3Object postObject(String bucketName, String key, byte[] data,
+ String contentType, Map<String, String> metadata) {
+ return postObject(bucketName, key, data, contentType, metadata, new PutObjectOptions());
+ }
+
+ public S3Object postObject(String bucketName, String key, byte[] data,
+ String contentType, Map<String, String> metadata, PutObjectOptions options) {
+ return createObject(bucketName, key, data, contentType, metadata, options, "ObjectCreated:Post");
+ }
+
+ private S3Object createObject(String bucketName, String key, byte[] data,
+ String contentType, Map<String, String> metadata,
+ PutObjectOptions options, String eventName) {
S3Object object = storeObject(bucketName, key, data, contentType, metadata, null, null, options);
- fireNotifications(bucketName, key, "ObjectCreated:Put", object);
+ fireNotifications(bucketName, key, eventName, object);
return object;
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java
index aa0b175e48..b9d4663ff4 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java
@@ -69,6 +69,113 @@ class S3PresignedPostIntegrationTest {
.body(equalTo(fileContent));
}
+ @Test
+ @Order(15)
+ void presignedPostEmitsPostNotification() {
+ String postQueueName = "presigned-post-notification-queue";
+ String putQueueName = "presigned-put-notification-queue";
+ String wildcardQueueName = "presigned-wildcard-notification-queue";
+ String postQueueUrl = createQueue(postQueueName);
+ String putQueueUrl = createQueue(putQueueName);
+ String wildcardQueueUrl = createQueue(wildcardQueueName);
+
+ try {
+ given()
+ .contentType("application/xml")
+ .queryParam("notification", "")
+ .body("""
+ <NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
+ <QueueConfiguration>
+ <Id>post-notification</Id>
+ <Queue>arn:aws:sqs:us-east-1:000000000000:%s</Queue>
+ <Event>s3:ObjectCreated:Post</Event>
+ </QueueConfiguration>
+ <QueueConfiguration>
+ <Id>put-notification</Id>
+ <Queue>arn:aws:sqs:us-east-1:000000000000:%s</Queue>
+ <Event>s3:ObjectCreated:Put</Event>
+ </QueueConfiguration>
+ <QueueConfiguration>
+ <Id>wildcard-notification</Id>
+ <Queue>arn:aws:sqs:us-east-1:000000000000:%s</Queue>
+ <Event>s3:ObjectCreated:*</Event>
+ </QueueConfiguration>
+ </NotificationConfiguration>
+ """.formatted(postQueueName, putQueueName, wildcardQueueName))
+ .when()
+ .put("/" + BUCKET)
+ .then()
+ .statusCode(200);
+
+ given()
+ .multiPart("key", "uploads/notified.txt")
+ .multiPart("file", "notified.txt", "notified".getBytes(StandardCharsets.UTF_8), "text/plain")
+ .when()
+ .post("/" + BUCKET)
+ .then()
+ .statusCode(204);
+
+ assertQueueContainsPostNotification(postQueueUrl);
+ assertQueueContainsPostNotification(wildcardQueueUrl);
+
+ String putQueueResponse = given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "ReceiveMessage")
+ .formParam("QueueUrl", putQueueUrl)
+ .formParam("MaxNumberOfMessages", "1")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().body().asString();
+ assertThat(putQueueResponse, not(containsString("<Message>")));
+ } finally {
+ given()
+ .contentType("application/xml")
+ .queryParam("notification", "")
+ .body("<NotificationConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"/>")
+ .put("/" + BUCKET);
+
+ deleteQueue(postQueueUrl);
+ deleteQueue(putQueueUrl);
+ deleteQueue(wildcardQueueUrl);
+ }
+ }
+
+ private String createQueue(String queueName) {
+ return given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateQueue")
+ .formParam("QueueName", queueName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");
+ }
+
+ private void assertQueueContainsPostNotification(String queueUrl) {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "ReceiveMessage")
+ .formParam("QueueUrl", queueUrl)
+ .formParam("MaxNumberOfMessages", "1")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("ReceiveMessageResponse.ReceiveMessageResult.Message.Body",
+ containsString("\"eventName\":\"ObjectCreated:Post\""));
+ }
+
+ private void deleteQueue(String queueUrl) {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DeleteQueue")
+ .formParam("QueueUrl", queueUrl)
+ .post("/");
+ }
+
@Test
@Order(20)
void presignedPostWithBinaryData() {
@@ -515,6 +622,7 @@ class S3PresignedPostIntegrationTest {
void cleanupBucket() {
// Delete all objects
given().delete("/" + BUCKET + "/uploads/test-file.txt");
+ given().delete("/" + BUCKET + "/uploads/notified.txt");
given().delete("/" + BUCKET + "/uploads/binary-data.bin");
given().delete("/" + BUCKET + "/uploads/no-policy.txt");
given().delete("/" + BUCKET + "/uploads/typed-file.json");