Run a program when a new file lands in S3 or Azure Blob
The real job is usually not the download. It is "a file arrives, then the import starts". This guide covers the pieces of that chain: noticing the new file, making sure it is complete, running your program on it, and acting on the result. It checks the cloud parts against Microsoft Learn and the AWS documentation listed at the end; the program-launching advice is general good practice, and you should test it with your own program.
Step 1: Decide how you notice a new file
There are two ways. You can poll: a scheduled job lists the bucket or container and compares it with what it has already seen. Or you can use events: the storage service tells something that a file was created.
Both clouds offer events. Amazon S3 event notifications can go to Amazon SNS, Amazon SQS, AWS Lambda or Amazon EventBridge, and AWS says they are designed to be delivered at least once. Azure Blob Storage events are pushed through Azure Event Grid to Azure Functions, Logic Apps or your own HTTP listener. Microsoft describes the event-based Azure Functions Blob trigger as recommended because of its lower latency, and the older polling-based trigger as having higher latency.
The catch for a folder on your own server: those event handlers run in the cloud. Getting the file to an on-premises folder, and starting a local program, takes an extra component that downloads it, which is why many teams poll from the Windows machine instead.
Step 2: Make sure the file is complete
Large files are uploaded in pieces. For Azure, Microsoft says that to make sure the Microsoft.Storage.BlobCreated event fires only when a block blob is completely committed, you filter it for the PutBlob, PutBlockList, CopyBlob or FlushWithClose calls, which trigger it only after the data is fully committed.
Whatever you use, download to a temporary name in the target folder and rename the file when the download has finished and been checked. Your import program, and anything else watching the folder, then never sees a half file.
Step 3: Run the program safely
- Pass the file path as a separate argument to your program. Do not build a command line by pasting a file name into a shell string: a name that contains quotes or an ampersand could run something you did not intend.
- Run it under an account with only the rights it needs, and give it a working folder and a time limit so a hung program cannot block every later file.
- Keep the program's output (stdout and stderr) in a log next to the file name, so you can see what happened to each file.
- Make the program safe to run twice. S3 notifications are delivered at least once, and any retry can send the same file again.
Step 4: Let the exit code decide
Treat exit code 0 as success and anything else as failure: only on success mark the file done and, if you want, delete or move the source file. On failure keep the file, retry and alert someone. The download tools follow the same convention. AzCopy documents exit code 0 as a finished job and 1 as an error to look up in the log, and the AWS CLI documents 0 for success and 1 when one or more Amazon S3 transfers failed.
A failure should never look like a success. If the import program fails and the downloaded file is simply left in the folder, the next person finds it days later.
Step 5: Watch the whole chain
- Alert when a run fails, and also when no new file has arrived when one is expected.
- Alert when the credentials expire or the job has not run for a while.
- Record each file, when it arrived, when your program ran and what it returned.
Where BucketWatch fits
BucketWatch is coming soon. It watches an Azure Blob container or an S3 bucket from a Windows service, downloads each new file to a temporary name and renames it when it is complete and checked, and on the Pro plan starts your program, script or webhook with the file path as a separate argument. The exit code decides whether the file is marked done or kept for a retry, and a failure raises an alert. We have not set a launch date, and the steps above work today.
Sources
- Amazon S3 User Guide: Amazon S3 Event Notifications
- Amazon S3 User Guide: Event notification types and destinations
- Microsoft Learn: Reacting to Blob storage events
- Microsoft Learn: Azure Blob storage trigger for Azure Functions
- Microsoft Learn: Troubleshoot issues in AzCopy v10 (exit codes)
- AWS CLI User Guide: Return codes
Coming soon: BucketWatch
New cloud files, downloaded to your folder. BucketWatch is a Windows service that watches an Azure Blob container or an Amazon S3 bucket and downloads new files to a folder you choose, local or on a network share. The Pro plan can then start your import program on each file. It alerts you when anything fails. It is for companies whose partner, lab or bank drops files into cloud storage but whose software only reads a folder.
Get BucketWatch first
Join the list and get 25% off your first purchase when it goes on sale. We email you a discount code on launch day. One code per person, for new customers.
One email when it launches (plus the discount code). No other mail, and you can say stop at any time. See our privacy policy.