简体   繁体   中英

How to create a “folder-like” (i.e. PREFIX) object on S3 using the AWS CLI?

It is usually explained that there are no folders/directories on S3, which is true, however there are PREFIX objects. In some cases - eg using riofs to "mount" a filesystem to S3 - it could actually be useful to know how to create one of these.

Does anyone know if there is a "correct" way to do this with the AWS CLI? It's likely possible using the low-level API aws s3api ...

Related SO posts:

Amazon S3 boto - how to create a folder?

Creating a folder via s3cmd (Amazon S3)

PS

I also want to point out that in the AWS console this action is actually named "Create Folder...", so it's not really fair to tell people that there is no "concept" of a folder on S3.

Many thanks!

After some quick fiddling around I found this seems to work:

aws s3api put-object --bucket test --key dir-test/

But it only works if you include the "/" at the end of the argument to --key . That part just didn't feel right... surely there's a better way?

There really, really aren't directories in S3. However, as you point out, there are prefixes and delimiters and the default delimiter is / which allows you to get a pretty convincing simulation of a hierarchical directory structure in an S3 bucket.

But the bucket is still just a flat space containing objects with key names. If you want to create a directory you have to create an object with a key whose name includes or ends with a delimiter character ( / by default). So your technique described above may not feel right but it is the only way.

And whoever came up with the Create Folder idea in the console should be ashamed of themselves. It causes a lot of confusion.

Just Save Folder name before you copy/move and add /DirName as destination

DirName = "Hello"
s3bucket = "s3://mybucket"


os.system("aws s3 cp " + DirName + " " + s3//mybucket" + "/" + DirName + " --recursive")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM