AWS S3 Sync with JS/Node SDK
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The AWS CLI gives you aws s3 sync, but the Node.js SDK does not provide one built-in command that mirrors a directory automatically. If you want sync behavior in application code, you need to implement it yourself by listing local files, listing S3 objects, comparing them, and then uploading or deleting as needed.
Define What "Sync" Means
Before writing any code, decide on the policy. A sync tool usually answers four questions:
- Should missing local files be uploaded?
- Should changed local files overwrite remote objects?
- Should remote objects that no longer exist locally be deleted?
- How are local paths converted into S3 object keys?
Without those rules, "sync" is just a vague label. The SDK gives you S3 operations, not the policy layer.
Uploading a Single File
The modern Node.js choice is AWS SDK v3. A single-file upload is straightforward:
That is only one building block. A sync feature needs discovery and comparison logic around it.
Listing Remote Objects
To compare local state with S3, first list the remote objects for the prefix you care about.
Pagination matters. A script that works for twenty files but ignores continuation tokens will silently fail on larger buckets.
Walking the Local Directory
You also need a local inventory. Node's filesystem APIs can walk the directory tree and generate relative paths that become S3 keys.
This preserves the directory structure by turning relative file paths into S3 keys such as images/logo.png.
Comparing Local and Remote State
A basic one-way sync can start with a size comparison. If the key does not exist remotely, or the size differs, upload the local file.
Size comparison is simple, not perfect. A serious sync tool may also compare checksums, timestamps, or even metadata. Still, size-only comparison is a good starting point for a custom application workflow.
Optional Delete Logic
If you want mirror semantics, delete remote keys that no longer exist locally.
Deletion is the risky part. Many teams deliberately ship upload-only sync first, then add delete mode only after the comparison logic is well tested.
Common Pitfalls
- Assuming the JavaScript SDK has a direct equivalent to
aws s3 sync. It does not. - Ignoring pagination when listing S3 objects.
- Comparing only filenames instead of full relative paths that map to keys.
- Writing delete logic too early and removing objects unintentionally.
- Hardcoding credentials instead of using the AWS credential chain, environment variables, or IAM roles.
Summary
- S3 sync in Node.js is something you build from list, compare, upload, and optional delete operations.
- AWS SDK v3 gives you the primitives, not a one-call sync feature.
- Preserve relative paths carefully when converting files into S3 object keys.
- Handle pagination and use a clear comparison strategy for changed files.
- Start with upload-only behavior unless you are confident your delete rules are correct.

