Download new files from an S3 bucket to a Windows folder
A partner, lab or vendor drops files into an Amazon S3 bucket, but the software that needs them only reads a folder. This guide shows how to pull the new files into that folder with the AWS CLI, which permissions to give, what to watch for, and how often to check. It checks each statement against the AWS documentation listed at the end; we have not run the commands for you, so try them on a test folder first.
Step 1: Make a read-only identity
Create a separate IAM user or role for this job and give it only what a download needs. AWS's example policies show the two parts: the s3:ListBucket action on the bucket itself, and object actions such as s3:GetObject on the objects in it (the bucket name followed by /*).
If the files live under one prefix (a folder-style key prefix), you can limit listing to it. AWS's example policies do this with the s3:prefix condition on the s3:ListBucket statement.
- Allow s3:ListBucket on the bucket, limited to the prefix with the s3:prefix condition.
- Allow s3:GetObject on the objects under that prefix.
- Do not allow s3:PutObject or s3:DeleteObject. A download job should not be able to change the bucket.
- Keep the access key out of scripts that are copied around; store it with the account that runs the job.
Step 2: Download the new files
The AWS CLI command aws s3 sync syncs S3 prefixes with directories. AWS describes it as recursively copying new and updated files from the source to the destination. A download looks like this: aws s3 sync s3://amzn-s3-demo-bucket/incoming/ D:\Inbox\ (replace the bucket name and prefix with yours).
It decides what is new by comparing each file with the copy in the folder. AWS documents the --size-only option (size is the only test) and the --exact-timestamps option (for downloads, same-sized files are skipped only when the timestamps match exactly) to change that rule, so read the two before you rely on the default. Do not add --delete unless you want files removed from the folder when they are no longer in the bucket; AWS documents that it deletes files in the destination that do not exist in the source.
Step 3: Know what a listing returns
A list request returns up to 1,000 objects at a time, and for general purpose buckets AWS returns them in lexicographical order of their key names, not by date. So "the newest file" is not the last one in the first page. A job that wants only new files has to remember what it has already downloaded, or compare against the folder as sync does, and it has to read every page of the listing.
Files stored in the S3 Glacier Flexible Retrieval or S3 Glacier Deep Archive storage classes are not immediately accessible. AWS says you must restore them before you can download them, so a bucket that moves old files to those classes needs a plan for them.
Step 4: Schedule it and read the result
Run the command from a scheduled task under an account that has the credentials and can write to the folder. Then look at the exit code, not only at whether the task ran. The AWS CLI documents code 0 for success and 1 when one or more Amazon S3 transfers failed. Code 2 means the command could not be parsed or, for S3 commands, that one or more files marked for transfer were skipped while all the others were transferred.
Write the command output to a log file, and send an email or chat message when the exit code is not 0. A job that nobody reads fails quietly for weeks.
- Download to a temporary folder and move finished files into the folder your software reads, so it never opens a half-written file.
- Check that a new file arrived when you expect one, and alert when it did not. A quiet bucket and a broken job look the same.
- Set a reminder for when the access key will be rotated or removed.
Step 5: If you need it faster
Amazon S3 can send event notifications when an object is created, to Amazon SNS, Amazon SQS, AWS Lambda or Amazon EventBridge. AWS says the notifications are designed to be delivered at least once and are typically delivered in seconds but can sometimes take a minute or longer, so the receiver must cope with a duplicate. Those destinations run in the cloud, so getting the file onto a folder in your office takes a little more work than the scheduled download above.
Where BucketWatch fits
BucketWatch is coming soon and is meant to do this without a script: you pick the bucket and prefix, a read-only access key and a folder, and it downloads each new file, remembers what it has, checks each download and alerts you when something fails. The Pro plan can also start your import program on each file. We have not set a launch date, and the manual steps above work today.
Sources
- AWS CLI Command Reference: aws s3 sync
- AWS CLI User Guide: Return codes
- Amazon S3 API Reference: ListObjectsV2
- Amazon S3 User Guide: Example bucket policies and user policies
- Amazon S3 User Guide: Working with archived objects (restoring)
- Amazon S3 User Guide: Amazon S3 Event Notifications
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.