I want to create an empty folder in amazon S3 using the ruby sdk. Ive read that there is no folder concept in S3, so theoretically to create a folder you would just create an empty object with a trailing "/"
我想使用ruby sdk在amazon S3中创建一个空文件夹。我已经读过S3中没有文件夹概念,所以理论上创建一个文件夹就可以创建一个带有尾部“/”的空对象
s3 = Aws::S3::Client.new( region: 'eu-west-1',
credentials: creds)
s3.put_object(bucket: "my_bucket",
key: "my_folder/")
Doing that creates an empty object on my bucket, but then if I try to upload a file like this:
这样做会在我的存储桶上创建一个空对象,但如果我尝试上传这样的文件:
s3.put_object(bucket: "my_bucket",
key: "my_folder/myfile")
It doesnt create a file in my_folder. It maintains the old empty object, and creates a folder and a file. So after the two commands the bucket structure is:
它不会在my_folder中创建文件。它维护旧的空对象,并创建一个文件夹和一个文件。所以在这两个命令之后,桶结构是:
my_bucket/
my_folder
my_folder/
my_file
Why is this happening? why does it create the object my_folder twice? How should I create an empty folder for later use?
为什么会这样?为什么它会创建对象my_folder两次?我应该如何创建一个空文件夹供以后使用?
3 个解决方案
#1
9
The Amazon S3 virtual folders spring into existence when any key contains a '/'. When browsing a bucket in the S3 console, it scans object keys for common prefixes and then uses that prefix to show a subset of a bucket.
当任何密钥包含'/'时,Amazon S3虚拟文件夹就会存在。在S3控制台中浏览存储桶时,它会扫描对象键以查找公共前缀,然后使用该前缀显示存储桶的子集。
Given the following object keys:
给定以下对象键:
- photos/family/reunion.jpg
- photos/family/vacation.jpg
- videos/funny.mp4
Then Amazon S3 would show the top level folders "photos" and "videos". If you delete the "videos/funny.mp4" object, then the "videos" directory would disappear.
然后,Amazon S3将显示*文件夹“照片”和“视频”。如果删除“videos / funny.mp4”对象,则“视频”目录将消失。
#2
2
As you say, S3 does not have the concept of a folder, "empty" or not, it just has objects which can have a "/" in them to mimic the naming convention of folders in a file system.
正如你所说,S3没有文件夹的概念,“空”或者没有,它只有一些对象,其中可以有“/”来模仿文件系统中文件夹的命名约定。
"my_folder" is not being treated twice, there are just two objects with a name that begins "my_folder/".
“my_folder”没有被处理两次,只有两个名称以“my_folder /”开头的对象。
What you'll have to do if you want to persist with this paradigm of having-folders-where-there-are-none is to delete your "my_folder/" object as soon as you create the first "my_folder/*" object.
如果你想坚持使用这种范式的文件夹,你必须要做的事情就是在你创建第一个“my_folder / *”对象时立即删除你的“my_folder /”对象。
And you should really not be asking, "How should I create an empty folder for later use?" when you acknowledge that folders do not exist.
你真的不应该问,“我应该如何创建一个空的文件夹供以后使用?”当您确认文件夹不存在时。
#3
1
S3 behaves as expected. It does not support folders. When you create file with name my_folder/myfile
, S3 creates object with key my_folder/myfile
. The idea of "folders" is built around access with prefix. So that, you can list object in "folder" like that:
S3表现如预期。它不支持文件夹。当您使用名称my_folder / myfile创建文件时,S3使用密钥my_folder / myfile创建对象。 “文件夹”的概念是围绕使用前缀的访问构建的。这样,您可以在“文件夹”中列出对象:
bucket.objects.with_prefix('my_folder').collect(&:key)
# => ["my_folder/myfile"]
#1
9
The Amazon S3 virtual folders spring into existence when any key contains a '/'. When browsing a bucket in the S3 console, it scans object keys for common prefixes and then uses that prefix to show a subset of a bucket.
当任何密钥包含'/'时,Amazon S3虚拟文件夹就会存在。在S3控制台中浏览存储桶时,它会扫描对象键以查找公共前缀,然后使用该前缀显示存储桶的子集。
Given the following object keys:
给定以下对象键:
- photos/family/reunion.jpg
- photos/family/vacation.jpg
- videos/funny.mp4
Then Amazon S3 would show the top level folders "photos" and "videos". If you delete the "videos/funny.mp4" object, then the "videos" directory would disappear.
然后,Amazon S3将显示*文件夹“照片”和“视频”。如果删除“videos / funny.mp4”对象,则“视频”目录将消失。
#2
2
As you say, S3 does not have the concept of a folder, "empty" or not, it just has objects which can have a "/" in them to mimic the naming convention of folders in a file system.
正如你所说,S3没有文件夹的概念,“空”或者没有,它只有一些对象,其中可以有“/”来模仿文件系统中文件夹的命名约定。
"my_folder" is not being treated twice, there are just two objects with a name that begins "my_folder/".
“my_folder”没有被处理两次,只有两个名称以“my_folder /”开头的对象。
What you'll have to do if you want to persist with this paradigm of having-folders-where-there-are-none is to delete your "my_folder/" object as soon as you create the first "my_folder/*" object.
如果你想坚持使用这种范式的文件夹,你必须要做的事情就是在你创建第一个“my_folder / *”对象时立即删除你的“my_folder /”对象。
And you should really not be asking, "How should I create an empty folder for later use?" when you acknowledge that folders do not exist.
你真的不应该问,“我应该如何创建一个空的文件夹供以后使用?”当您确认文件夹不存在时。
#3
1
S3 behaves as expected. It does not support folders. When you create file with name my_folder/myfile
, S3 creates object with key my_folder/myfile
. The idea of "folders" is built around access with prefix. So that, you can list object in "folder" like that:
S3表现如预期。它不支持文件夹。当您使用名称my_folder / myfile创建文件时,S3使用密钥my_folder / myfile创建对象。 “文件夹”的概念是围绕使用前缀的访问构建的。这样,您可以在“文件夹”中列出对象:
bucket.objects.with_prefix('my_folder').collect(&:key)
# => ["my_folder/myfile"]