Skip to main content

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
  1. A file is uploaded to the S3 bucket.
  2. S3 sends a JSON event notification to the SQS queue.
  3. The sqs:Listener polls the queue, receives the message, and invokes onMessage.
  4. Your service parses the S3 event and logs the event details.

Before you begin​

Prerequisites
  • 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, and sqs:GetQueueAttributes permissions 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​

  1. Open the SQS Console and select Create queue.
  2. Choose Standard queue type.
  3. Give the queue a name, for example s3-events, and complete the creation.
  4. 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​

  1. Open the S3 Console and select Create bucket.
  2. 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>.

Finding your AWS account ID

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.

  1. Open your bucket and go to the Properties tab.
  2. Scroll to Event notifications and select Create event notification.
  3. Enter an Event name, for example object-created.
  4. Under Event types, expand Object creation and select All object create events (s3:ObjectCreated:*).
  5. Under Destination, select SQS queue. Choose Choose from your SQS queues and select the s3-events queue you created in Step 1.
  6. Select Save changes.
Verify the setup

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.

Create the s3-file-event-listener project

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:

  1. 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"
    }
    }
    }

    Import JSON to generate the S3EventRecord type

  2. Select Import to generate the types. This creates the S3EventRecord type along with its nested types (S3, Bucket, and Object). Generated S3EventRecord type diagram

  3. 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.

    Allow Additional Fields checked for generated types

  4. Select + Add Type and choose Create from scratch. Set the name to S3Notification. Select the + icon on fields, set the field name to Records, and set the type to S3EventRecord[]. Check Allow Additional Fields for this type as well.

    Create S3Notification type from scratch

  5. Select Save. The complete type diagram shows the S3Notification type linked to the S3EventRecord array and its nested types.

    Complete type diagram with S3Notification

Step 7: Add an AWS SQS Trigger​

  1. Go to the integration home and select Add Artifact. Under Event Integration, Select the AWS SQS trigger to open the listener configuration form.

  2. Fill in the connection parameters:

    FieldValue
    RegionThe AWS region where your SQS queue is located, for example us-east-1
    Access Key IDYour 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 KeyThe 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 URLThe full URL you copied from the SQS queue details page in Step 1, for example https://sqs.us-east-1.amazonaws.com/123456789012/s3-events
    Poll Interval30 (seconds between polls)
    Wait Time20 (long-poll duration in seconds)
    Visibility Timeout30 (seconds a received message is hidden from other consumers)
    Best practice

    Bind accessKeyId, secretAccessKey, and queueUrl to configurable variables rather than hardcoding them. In the expression editor, select Configurables → New Configurable to create a runtime-supplied value.

    SQS trigger configurations

Step 8: Implement the service callbacks​

Switch to the Ballerina Code tab to see the full source, or continue building in the Visual Designer.

  1. 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 onMessage callback.

    Add the onMessage handler to the SQS trigger

  2. Open the onMessage callback. Add a printInfo node under the Logging section with the message "New S3 events are received" to log when new events arrive.

    Add a log node for new S3 events

  3. Add a Declare Variable node — name it body, set the type to string, and set the expression to check message.body.ensureType().

    Declare the body variable

  4. Add a Call Function node and search for the fromJsonStringWithType function.

    Search for the fromJsonStringWithType function

    For Str, select body from variables. Set the Result variable name to notification, and set T* to S3Notification.

    Configure the fromJsonStringWithType function parameters

  5. Add a Foreach node — set the collection to notification.Records, the variable name to eventRecord, and the variable type to S3EventRecord.

    Add a foreach loop over notification events

  6. 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}`

    Log each event record with brief info

  7. The complete onMessage flow should look like this:

    Complete onMessage callback flow


Step 9: Run and verify​

  1. Open Configurations in WSO2 Integrator and supply your AWS credentials and queue URL.
  2. Run the integration.
  3. 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"
Troubleshooting
  • No messages appear — verify the SQS access policy includes the S3 SendMessage permission and the aws:SourceArn condition matches your bucket ARN exactly.
  • Parse error — print the raw message.body value 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:TestEvent message when you first configure event notifications. This message has a different structure and does not contain a Records array. 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.s3 connector's getObject action to download the file content inside onMessage, then parse it with ballerina/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?