Create an empty S3 ruby ​​SDK folder

I want to create an empty folder in amazon S3 using ruby ​​sdk. Ive read that in S3 there is no concept of folders, so theoretically to create a folder you would just create an empty object with the ending "/"

s3 = Aws::S3::Client.new( region: 'eu-west-1', credentials: creds) s3.put_object(bucket: "my_bucket", key: "my_folder/") 

Doing this creates an empty object in my bucket, but then if I try to upload the file as follows:

 s3.put_object(bucket: "my_bucket", key: "my_folder/myfile") 

It does not create a file in my folder. It maintains an old empty object and creates a folder and file. Therefore, after two commands, the structure of the bucket:

 my_bucket/ my_folder my_folder/ my_file 

Why is this happening? why does it create my_folder object twice? How to create an empty folder for future use?

+6
source share
3 answers

Amazon S3 spring virtual folders exist when any key contains "/". When viewing a bucket in the S3 console, it scans the object keys for common prefixes, and then uses this prefix to display a subset of the bucket.

Given the following object keys:

  • photo / family / reunion.jpg
  • photo / family / vacation.jpg
  • video /funny.mp4

Amazon S3 will then display the top-level folders for β€œphotos” and β€œvideos.” If you delete the object "videos / funny.mp4", then the directory "videos" will disappear.

+9
source

As you say, S3 does not have the concept of a folder β€œempty” or not, it just has objects that can have β€œ/” in them to simulate a folder naming convention in the file system.

"my_folder" is not processed twice, there are only two objects with a name that begins with "my_folder /".

What you need to do if you want to keep this paradigm of having -folders-where-there-no-no is to delete your "my_folder /" object as soon as you create the first "my_folder / *".

And you really shouldn't be asking, "How do I create an empty folder for future use?" when you confirm that the folders do not exist.

+1
source

S3 behaves as expected. It does not support folders. When you create a file called my_folder/myfile , S3 creates an object with the key my_folder/myfile . The idea of ​​"folders" is built around access with a prefix. Thus, you can list the object in the "folder" as follows:

 bucket.objects.with_prefix('my_folder').collect(&:key) # => ["my_folder/myfile"] 
0
source

Source: https://habr.com/ru/post/983883/


All Articles