File Integrations on S3 File Events
This integration triggers an action each time a file is uploaded to an Amazon S3 bucket. AWS does not have a built-in trigger for S3, and one of the recommended approaches is to publish these events to an AWS SQS queue. S3 publishes an object-created notification to the queue, and the AWS SQS trigger consumes it — so your service reacts within seconds of the upload without polling S3.
What you'll build: A Ballerina integration that reacts every time a file is uploaded to an S3 bucket. S3 pushes a notification to an SQS queue; the aws.sqs listener picks it up and your service logs the S3 event details without polling S3 directly.
How it works
S3 Bucket ──(ObjectCreated)──► SQS Queue ──(poll)──► sqs:Listener ──► onMessage callback
- A file is uploaded to the S3 bucket.
- S3 sends a JSON event notification to the SQS queue.
- The
sqs:Listenerpolls the queue, receives the message, and invokesonMessage. - Your service parses the S3 event and logs the event details.
Before you begin
- An AWS account with permissions to manage S3, SQS, and IAM.
- A working WSO2 Integrator environment. See Setup.
- AWS Access Key ID and Secret Access Key for an IAM user who has the necessary
sqs:ReceiveMessage,sqs:DeleteMessage, andsqs:GetQueueAttributespermissions on your SQS queue. See the AWS SQS Setup Guide if you need to create credentials.
Part 1: Configure AWS
Step 1: Create the SQS queue
- Open the SQS Console and select Create queue.
- Choose Standard queue type.
- Give the queue a name, for example
s3-events, and complete the creation. - Once created, open the queue and copy the URL shown in the details panel (for example
https://sqs.us-east-1.amazonaws.com/123456789012/s3-events). You will need this value in Step 7.
Step 2: Create the S3 bucket
- Open the S3 Console and select Create bucket.
- Enter a unique bucket name. When choosing a bucket namespace, select the Account Regional namespace (recommended), which ensures the bucket name is unique to your account and region. If you use the default global namespace instead, the name must be globally unique. Learn more about bucket namespaces.
Step 3: Grant S3 permission to write to the queue
S3 needs explicit permission to publish messages to your queue. Open the queue you created in Step 1, go to Access policy, and add the following statement inside the Statement array of the existing policy. Replace the placeholders with your own values — use the bucket name you chose in Step 2 for <your-bucket-name>.
Your 12-digit account ID appears in the top-right corner of the AWS Console when you select your account name. You can also find it in the queue ARN shown on the queue details page (for example arn:aws:sqs:us-east-1:123456789012:s3-events — here 123456789012 is the account ID).
{
"Sid": "allow-s3-send-message",
"Effect": "Allow",
"Principal": {
"Service": "s3.amazonaws.com"
},
"Action": "SQS:SendMessage",
"Resource": "arn:aws:sqs:<region>:<account-id>:s3-events",
"Condition": {
"ArnLike": {
"aws:SourceArn": "arn:aws:s3:::<your-bucket-name>"
},
"StringEquals": {
"aws:SourceAccount": "<account-id>"
}
}
}
Save the updated policy.
Step 4: Configure S3 event notifications
Link the bucket to the queue so S3 knows where to send events.
- Open your bucket and go to the Properties tab.
- Scroll to Event notifications and select Create event notification.
- Enter an Event name, for example
object-created. - Under Event types, expand Object creation and select All object create events (
s3:ObjectCreated:*). - Under Destination, select SQS queue. Choose Choose from your SQS queues and select the
s3-eventsqueue you created in Step 1. - Select Save changes.
Upload any .csv file to the bucket. Open the SQS console, select Send and receive messages, and click Poll for messages. You should see a JSON message appear. You may also see a one-off s3:TestEvent message immediately after configuration — that is normal and can be safely deleted.
Part 2: Build the Ballerina integration
Step 5: Create the integration
Create a new integration project in WSO2 Integrator. Add the Integration Name as s3-file-event-listener.

Step 6: Create the record types
Define the record types that map to the S3 event notification JSON structure. These types let Ballerina parse the incoming SQS message body into typed records.
Add two Record Type artifacts to the project:
-
Go to Add Artifact and select Type. Select the Import tab and set the name to
S3EventRecord. Provide a JSON sample like below:{"eventSource": "aws:s3","eventName": "ObjectCreated:Put","eventTime": "2026-08-21T07:30:00Z","awsRegion": "us-east-1","s3": {"bucket": {"name": "my-example-bucket","arn": "arn:aws:s3:::my-example-bucket"},"object": {"key": "documents/example.txt","size": 1024,"eTag": "9b2cf535f27731c974343645a3985328"}}}
-
Select Import to generate the types. This creates the
S3EventRecordtype along with its nested types (S3,Bucket, andObject).
-
Make sure Allow Additional Fields is checked for each generated type, since the actual S3 notification JSON can contain additional fields beyond the ones defined here.

-
Select + Add Type and choose Create from scratch. Set the name to
S3Notification. Select the + icon on fields, set the field name toRecords, and set the type toS3EventRecord[]. Check Allow Additional Fields for this type as well.
-
Select Save. The complete type diagram shows the
S3Notificationtype linked to theS3EventRecordarray and its nested types.
Step 7: Add an AWS SQS Trigger
-
Go to the integration home and select Add Artifact. Under Event Integration, Select the AWS SQS trigger to open the listener configuration form.
-
Fill in the connection parameters:
Field Value Region The AWS region where your SQS queue is located, for example us-east-1Access Key ID Your AWS Access Key ID (use a configurable). To create these credentials, go to the IAM Console → Users → select your user → Security credentials tab → Create access key. See the AWS SQS Setup Guide for detailed steps. Secret Access Key The secret key generated alongside the Access Key ID above (use a configurable). Copy it when it is first shown — AWS does not display it again. Queue URL The full URL you copied from the SQS queue details page in Step 1, for example https://sqs.us-east-1.amazonaws.com/123456789012/s3-eventsPoll Interval 30(seconds between polls)Wait Time 20(long-poll duration in seconds)Visibility Timeout 30(seconds a received message is hidden from other consumers)Best practiceBind
accessKeyId,secretAccessKey, andqueueUrlto configurable variables rather than hardcoding them. In the expression editor, select Configurables → New Configurable to create a runtime-supplied value.
Step 8: Implement the service callbacks
Switch to the Ballerina Code tab to see the full source, or continue building in the Visual Designer.
- Visual Designer
- Ballerina Code
-
After adding the SQS Trigger artifact and configuring the listener as described in Step 7, select + Add Handler and choose On Message to add the
onMessagecallback.
-
Open the
onMessagecallback. Add a printInfo node under the Logging section with the message"New S3 events are received"to log when new events arrive.
-
Add a Declare Variable node — name it
body, set the type tostring, and set the expression tocheck message.body.ensureType().
-
Add a Call Function node and search for the
fromJsonStringWithTypefunction.
For Str, select
bodyfrom variables. Set the Result variable name tonotification, and set T* toS3Notification.
-
Add a Foreach node — set the collection to
notification.Records, the variable name toeventRecord, and the variable type toS3EventRecord.
-
Inside the foreach loop, add a printInfo node under the Logging section. Set the message to a brief summary of the event using the following expression:
string `Event: ${eventRecord.eventName}, Bucket: ${eventRecord.s3.bucket.name}, File: ${eventRecord.s3.'object.'key}`
-
The complete
onMessageflow should look like this:
The integration produces the following files. Create or update them as shown below.
Ballerina.toml — the package descriptor for the integration:
[package]
org = "wso2"
name = "s3_file_event_listener"
version = "0.1.0"
distribution = "2201.13.4"
[build-options]
sticky = true
Config.toml — supply your actual AWS credentials and queue URL at runtime (never commit this file):
accessKeyId = "<your-access-key-id>"
secretAccessKey = "<your-secret-access-key>"
# Use the queue URL you copied in Step 1
queueUrl = "https://sqs.us-east-1.amazonaws.com/<account-id>/s3-events"
types.bal — types that mirror the S3 event notification structure:
public type Bucket record {
string name;
string arn;
};
public type Object record {
string 'key;
int size;
string eTag;
};
public type S3 record {
Bucket bucket;
Object 'object;
};
public type S3EventRecord record {
string eventSource;
string eventName;
string eventTime;
string awsRegion;
S3 s3;
};
type S3Notification record {
S3EventRecord[] Records;
};
config.bal — configurable variables supplied via Config.toml at runtime:
configurable string accessKeyId = ?;
configurable string secretAccessKey = ?;
configurable string queueUrl = ?;
main.bal — the listener and service logic:
import ballerina/lang.value;
import ballerina/log;
import ballerinax/aws.sqs;
listener sqs:Listener sqsListener = new (
{
region: "us-east-1",
auth: {
accessKeyId: string `${accessKeyId}`,
secretAccessKey: string `${secretAccessKey}`
}
},
{
pollInterval: 30,
waitTime: 20,
visibilityTimeout: 30
}
);
@sqs:ServiceConfig {queueUrl: string `${queueUrl}`}
service sqs:Service on sqsListener {
remote function onMessage(sqs:Message message) returns error? {
do {
log:printInfo("New S3 events are received");
string body = check message.body.ensureType();
S3Notification notification = check value:fromJsonStringWithType(string `${body}`);
foreach S3EventRecord eventRecord in notification.Records {
log:printInfo(string `Event: ${eventRecord.eventName}, Bucket: ${eventRecord.s3.bucket.name}, File: ${eventRecord.s3.'object.'key}`);
}
} on fail error err {
// handle error
return error("unhandled error", err);
}
}
}
Step 9: Run and verify
- Open Configurations in WSO2 Integrator and supply your AWS credentials and queue URL.
- Run the integration.
- Upload a file to your S3 bucket.
Within seconds the terminal should print output similar to:
time=2026-08-26T16:05:44.852+05:30 level=INFO module=user/s3_and_sqs_integration message="New S3 events are received"
time=2026-08-26T16:05:44.852+05:30 level=INFO module=user/s3_and_sqs_integration message="Event: ObjectCreated:Put, Bucket: my-example-bucket, File: documents/example.txt"
- No messages appear — verify the SQS access policy includes the S3
SendMessagepermission and theaws:SourceArncondition matches your bucket ARN exactly. - Parse error — print the raw
message.bodyvalue and compare it against the S3 Event Message Structure to check if field names have changed. - Test event causes a parse failure — S3 sends a one-off
s3:TestEventmessage when you first configure event notifications. This message has a different structure and does not contain aRecordsarray. You can safely delete it from the SQS console.
What's next
Now that your listener is consuming S3 events, extend the integration further:
- Read the CSV from S3 — use the
ballerinax/aws.s3connector'sgetObjectaction to download the file content insideonMessage, then parse it withballerina/data.csv. - Filter by prefix — add a prefix filter (for example
uploads/) to the S3 event notification configuration in Step 4 so only files under that path trigger events. - Fan out to multiple consumers — subscribe additional SQS queues to an SNS topic and have the S3 bucket notify the topic instead. Each downstream queue can run a separate Ballerina listener.
- Deploy the integration — ship to WSO2 Cloud, a Docker container, or Kubernetes for production use.
Was this page helpful?