I want to create folder in S3 bucket from Ec2 instacne . I tried the put object but its not working . Is there any way of creating folder on s3 from ec2 instace using cli.
-
2Possible duplicate of [Add folder in Amazon s3 bucket](http://stackoverflow.com/questions/6791257/add-folder-in-amazon-s3-bucket) – Anthony Neace Apr 25 '16 at 12:26
6 Answers
You don't need to create a folder to put item in it. For example, just run something like the below command and s3 will create the folders if they don't exist:
aws s3 cp ./testfile s3://yourBucketName/any/path/you/like
If you want to use cp recursively you can specify --recursive option, or use "aws s3 sync".
If your command does not work, then you may have permission issues. Paste your error so that we can help you.

- 383
- 5
- 20

- 2,689
- 16
- 21
-
8Just a small usage point: If you copy a file as above you end up with a file called s3://yourBucketName/any/path/you/like If you add a following / to the path (after the like) you end up with a file called testfile within s3://yourBucketName/any/path/you/like/ – Paul Pritchard Feb 19 '18 at 15:46
-
Perhaps obvious to some, but you must have a file called 'testfile' in your current working directory for the command in the answer to work. Quickly make such a file in a bash-like terminal by executing 'touch testfile'. – Joshua T Mar 23 '20 at 16:44
aws s3api put-object --bucket bucketname --key foldername/
This command works like a charm. Courtesy AWS Support.

- 6,052
- 10
- 43
- 117

- 481
- 4
- 3
-
1Make sure not to omit the trailing `/` at the end of `foldername/`. If you do, S3 will create an empty object called `foldername`. – teuber789 Mar 04 '23 at 01:36
aws s3 sync <folder_name> s3://<you-prefix>/<some_other_folder>/<folder_name>

- 20,061
- 36
- 171
- 301
And bare in mind that, S3 is an object store. It doesn't deal with folder.
If you create /xyz/ and upload a file call /xyz/foo.txt , those are actually 2 different object. if you delete /xyz/ , it will not delete /xyz/foo.txt.
S3 console allow you to "create folder", but after you play with it, you will notice , you CANNOT RENAME folder, or do ANYTHING that you can play with a folder (like moving a tree structure, recursively specify access rights)
In S3, there is something call "PREFIX" where the API allow you to list/filter file with particular "prefix", that let you deal with abstraction.
As mentioned above, since you CANNOT do anything like a file system folder, if you want to perform task like moving one folder to another folder, You need to write your own code to "rewrite" the file name(To be specific, it is "Key" in S3) , i.e. copy it to new object name and delete the old object.
If you want build advance control on S3, you may choose any of the AWS SDK to do it. https://aws.amazon.com/tools/
You can play around with the API function call put_object() (naming varied depends on SDK language) and proof those facts (which most is found inside AWS documentation)
update: Since @Tom raise up the issues.
You cannot create an virtual folder using AWS cli (Maybe @Tom can show how), only ways to do that is using AWS SDK put_object()
Let's try this First I create dummy file in shell
echo "dummy">test.txt
Then try use python aws sdk
import boto3
s3=boto3.client("s3")
s3.create_bucket(Bucket="dummy")
# now create so call xyz/ "empty virtual folder"
s3.put_object(Bucket="dummy", Key="xyz/")
# now I put above file name to S3 , call xyz/test.txt
# First I must open the file, because put_object only take bytes or file object
myfile=open("test.txt")
s3.put_object(Bucket="dummy", Key="xyz/test.txt")
Now, go to your command shell, fire up your AWS CLI (or continue to play with boto3)
# check everything
aws s3 ls s3://dummy --recursive
#now delete the so call "folder"
aws s3 rm s3://dummy/xyz/
# And you see the file "xyz/test.txt" is still there
aws s3 s3://dummy --recursive

- 12,845
- 5
- 47
- 44
-
that's not totally accurate: if you create /xyz/ and upload a file called /xyz/foo.txt, then /xyz/ is not considered as an object and you can't remove it (you would get a 404 with Key "/xyz/" does not exist). the real key is /xyz/foo.txt and the object is actually your file. – Tom Apr 25 '16 at 14:51
-
@Tom, I already write a proven script, please check whether you can invalidate that. – mootmoot Apr 25 '16 at 15:42
-
weird because based on my tests I get a 404. Also, based on the functioning of S3, 'xyz' would definitely not be a key as it corresponds to no real value of the key/value store. So removing it removes actually nothing. In S3, a folder is not a key but a part of the key. here is a gist link to my tests, and it is also possible that I am missing sth (never say 'never' :p ) so please let me know if you spot sth wrong on my side: https://gist.github.com/anonymous/7ee3cf435cfd503a333478f9018ea2f6 – Tom Apr 25 '16 at 16:21
-
@Tom , that's what I suspected, AWS CLI doesn't allow "empty folder name". That's the reason I create the boto3 code to show it is possible to make a "arbitary folder" (AKA empty key object) ;-D – mootmoot Apr 26 '16 at 09:58
-
So basically I think we say the same thing. boto3 is likely to hide this 404, but nothing actually happens on AWS side and the object remains available – Tom Apr 26 '16 at 11:01
-
@Tom , run the boto3 code I show above before making your conclusion. No need to guess, the PoC is right in front of you. – mootmoot Apr 26 '16 at 11:02
-
I don't want to know what boto3 is returning, because the fact is that this is an intermediate layer between the user and the API and it can hide or catch some errors. the fact that the CLI returns a 404 error demonstrates better the use of key/values by S3 to my mind. So I could say the exact same thing with my snippet, you get the PoC right in front of you as well :p – Tom Apr 26 '16 at 11:12
-
@Tom : I will rest the case on what the question ask. I am not sure about your intention for pointing out what you cannot do with AWS CLI. There is a limitation of individual tools, get over it. Like I didn't complain that boto3 doesn't use the --query filter in AWS CLI, it is the current tools limit. – mootmoot Apr 26 '16 at 11:20
-
I just mention that it is not the CLI that generates a 404 error, since this is an http error, and it is thus the code returned from the AWS API directly. Whatever, I think you are right saying we will not agree on this point so let stay on this (plus this is quite a minor point). cheers :p – Tom Apr 26 '16 at 12:07
-
-
of course this gist is CLI, that s what I mean from the beginning: AWS API answers a 404 http code, but boto3 is not raising an error :p I think this is turning to a useless discussion, and obviously you don't get my point and I am not getting yours neither, so I propose to stop there :) – Tom Apr 26 '16 at 12:42
-
Features that are NOT supported in AWS CLI doesn't mean it wouldn't work in AWS SDK API, vice versa. – mootmoot Apr 26 '16 at 12:57
-
You can find the commands here from official blog of AWS:
http://docs.aws.amazon.com/cli/latest/userguide/using-s3-commands.html
And there are different other tools available which can be used to create Bucket/ folders in S3. One of the known tool is S3Browser which is available for windows servers. Install it on your EC2 instance and provide your AWS access key and secret keys to access the S3. This tool provide simple UI to do that.

- 1,191
- 11
- 19
-
1I know quite a lot of SO users don't like answers with just one link, because once the link dies then the answer is not useful anymore. So maybe this is why you get downvoted – Tom Apr 25 '16 at 12:10
-
Okay, if this is the thing, I have updated my answer, now the down vote should be removed from my answer automatically. – error_handler Apr 25 '16 at 12:18
-
please note that downvotes are not automatic. You have been downvoted by a user that disliked your answer for some reason that he did not specify and I am only suggesting a plausible explanation for this. So unfortunately, until you get upvoted, your score will remain to '-1' – Tom Apr 25 '16 at 12:36
-
This still doesn't really answer the question as posed. The answer to 'How do I create a folder on S3?' is not 'Hey check out these tools and figure it out yourself.' Please check this meta discussion for guidance: http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Anthony Neace Apr 25 '16 at 15:40
There is no cli command that allows you to simply create a folder in an s3 bucket. To create this folder I would use the following command, which creates an empty file, with nothing inside. But if you delete the file you will delete the folder as long as you have not added anything else afterwards
aws s3api put-object --bucket bucket_name --key folder_name/empty.csv