简体   繁体   English

如何使用AWS CLI在S3上创建“类文件夹”(即PREFIX)对象?

[英]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. 通常会解释S3上没有文件夹/目录,这是真的,但是有PREFIX对象。 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. 在某些情况下 - 例如使用riofs将文件系统“挂载”到S3 - 知道如何创建其中一个实际上很有用。

Does anyone know if there is a "correct" way to do this with the AWS CLI? 有没有人知道使用AWS CLI是否有“正确”的方法来做到这一点? It's likely possible using the low-level API aws s3api ... 可能使用低级API aws s3api ...

Related SO posts: 相关SO帖子:

Amazon S3 boto - how to create a folder? Amazon S3 boto - 如何创建文件夹?

Creating a folder via s3cmd (Amazon S3) 通过s3cmd创建文件夹(Amazon S3)

PS 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. 我还想指出,在AWS控制台中,此操作实际上名为“创建文件夹...”,因此告诉人们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 . 但它只有在参数末尾包含“/” --key That part just didn't feel right... surely there's a better way? 那部分感觉不对......当然有更好的方法吗?

There really, really aren't directories in S3. 确实,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. 但是,正如您所指出的,有前缀和分隔符,默认分隔符是/ ,它允许您在S3存储桶中获得非常有说服力的模拟分层目录结构。

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添加为目标

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


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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