How to create folder or key on s3 using AWS SDK for Node.js?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Amazon S3 does not have real folders in the filesystem sense. It stores objects by key, and folder views in the console are just key prefixes separated by /. In Node.js, creating a folder usually means creating a zero-byte object whose key ends with /, or simply uploading objects under that prefix.
Understand Keys, Prefixes, and Folder Illusion
If you upload an object with key reports/2026/march.csv, S3 treats the full string as the object key. The console shows nested folders because it groups keys by delimiter. This means you can often skip explicit folder creation and write objects directly using the full key path.
For teams new to S3, this model explains why deleting a folder in the console often just removes objects with a shared prefix.
Setup AWS SDK for JavaScript v3
Use the modular SDK package and authenticate with IAM role, profile, or environment variables.
When running on EC2, ECS, or Lambda, prefer role-based credentials instead of hardcoded keys.
Create a Folder Marker Key
A folder marker is a zero-byte object ending in slash. It is optional, but can be useful when you want the prefix visible in tools before any real files exist.
This produces a key such as uploads/images/ with zero content.
Create an Object Under a Prefix
In most production systems, you can skip folder markers and upload the real object directly.
After upload, the console automatically shows the reports and 2026 folder levels.
Verify Prefix Contents
Listing by prefix helps confirm creation and supports admin tooling.
For large prefixes, handle pagination using ContinuationToken until IsTruncated is false.
Security and Design Guidance
Keep IAM policies scoped to required bucket paths. For example, an upload service may only need s3:PutObject under uploads/* and list access for one prefix. Avoid broad wildcard access for all buckets.
Choose key naming conventions early. Include stable partition fields such as tenant id, date, and file purpose. Predictable keys simplify lifecycle policies, analytics, and bulk cleanup.
Common Pitfalls
A common misunderstanding is expecting an API call named folder creation. In S3, folder behavior is key naming, not a separate resource type. Another issue is missing trailing slash when creating a marker object, which results in a regular object key instead of a visible folder-like node. Developers also forget content type metadata and later see incorrect behavior in downstream consumers. Finally, large list operations can become slow or expensive if prefixes are too broad, so design key partitions to keep list scopes narrow.
Summary
- S3 folders are key prefixes, not actual directories.
- Create folder markers with zero-byte keys ending in
/only when needed. - Most workflows can upload objects directly under a prefix.
- Use
ListObjectsV2with prefix filters to validate organization. - Apply least-privilege IAM and consistent key naming from day one.

