- Newest
- Most votes
- Most comments
There's no mechanism in CloudFormation that would create objects in your S3 bucket. Technically, folders are just zero-byte objects with a name ending in a forward slash '/', but they don't technically serve as the sorts of containers that folders are in a file system. Are you sure you need the mostly cosmetic, visual "folder structure", or are objects simply going to be uploaded "into folders"? In the latter case, no "empty folders" are needed.
You do not need to create folders in S3. Just name your objects with the structure that you want to appear as a folder structure.
See here for more info: https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-folders.html
In Amazon S3, buckets and objects are the primary resources, and objects are stored in buckets. Amazon S3 has a flat structure instead of a hierarchy like you would see in a file system. However, for the sake of organizational simplicity, the Amazon S3 console supports the folder concept as a means of grouping objects. The console does this by using a shared name prefix for the grouped objects. In other words, the grouped objects have names that begin with a common string. This common string, or shared prefix, is the folder name. Object names are also referred to as key names. For example, you can create a folder in the console named photos and store an object named myphoto.jpg in it. The object is then stored with the key name photos/myphoto.jpg, where photos/ is the prefix.
Here are two more examples:
If you have three objects in your bucket—logs/date1.txt, logs/date2.txt, and logs/date3.txt—the console will show a folder named logs. If you open the folder in the console, you will see three objects: date1.txt, date2.txt, and date3.txt.
If you have an object named photos/2017/example.jpg, the console will show you a folder named photos containing the folder 2017. The folder 2017 will contain the object example.jpg.
You can have folders within folders, but not buckets within buckets. You can upload and copy objects directly into a folder. Folders can be created, deleted, and made public, but they cannot be renamed. Objects can be copied from one folder to another.
Hope this helps!
Can I get sample cft for this
hello,
Understanding S3 and CloudFormation:
S3 Folders: S3 doesn't have folders in the traditional sense. It uses object keys with forward slashes (/) to mimic a hierarchical structure. You can create this structure by defining object keys with desired paths in your CloudFormation template.
CloudFormation Resources: CloudFormation doesn't provide a built-in resource to directly create objects within S3 buckets.
Leverage Object Keys: In your CloudFormation template, define resources for objects with keys that represent your desired folder structure. For example:
MyObject1: {
Type: "AWS::S3::Object",
Properties: {
Bucket: "mybucket",
Key: "path/to/folder1/MyObject1.txt" # Mimics a folder structure
}
}
MyObject2: {
Type: "AWS::S3::Object",
Properties: {
Bucket: "mybucket",
Key: "path/to/folder2/MyObject2.txt"
}
}
Here, "path/to/folder1/" and "path/to/folder2/" in the object keys create the illusion of folders in the S3 console.
Consider Alternatives: If you truly need separate logical entities within S3, explore options like:
S3 Object Lifecycle Management: Define lifecycle rules to transition objects to different storage classes based on prefixes (simulating folders). Amazon S3 Select and Glacier Select: Perform filtering and querying operations on specific prefixes within a bucket.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-s3-bucket.html
https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lifecycle-mgmt.html
Relevant content
- asked 3 years ago

Hi Leo, Can I get sample cft for this
Hi @Admin, you don't need anything in CloudFormation (CFN) to be able to upload files "to folders". Just upload the file into a nonexistent folder. When you look at the bucket's contents in the console, you will see the file inside the folder you specified, despite no folder actually existing or having been created. For example, if you upload a file to a completely empty bucket with the path
s3://s3-bucket-name/folder/filename.txt, when you look inside it via the console, you'll see a folder calledfolder/and inside it, you'll findfilename.txt.Hi Leo, Basically we have few files that needs to be uploaded using cft and not from cli