Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create empty folder S3 ruby SDK

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 "/"

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_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?

like image 284
gumlym Avatar asked Mar 16 '15 20:03

gumlym


People also ask

Can you create an empty folder in S3?

Creating a folder Instead, upload an empty folder and specify these settings in the upload configuration. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to create a folder in.

Can we create folder in S3 bucket?

Buckets are the top level containers in Amazon S3 for data storage. In order to start working with Amazon S3 you need to create at least one bucket. Once you have a buket, you may organize files by creating different folders inside the bucket to reflect the logical structure of your files.

Does AWS S3 CP create folder?

The Amazon S3 implements folder object creation by creating a zero-byte object. If you see a file in the console you will see the key of the file also has the folder reference in the key – test-folder/hdfs-0.0.

What is _$ folder in S3?

The "_$folder$" files are placeholders. Apache Hadoop creates these files when you use the -mkdir command to create a folder in an S3 bucket. Hadoop doesn't create the folder until you PUT the first object. If you delete the "_$folder$" files before you PUT at least one object, Hadoop can't create the folder.


Video Answer


3 Answers

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.

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.

like image 92
Trevor Rowe Avatar answered Oct 20 '22 22:10

Trevor Rowe


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.

"my_folder" is not being treated twice, there are just two objects with a name that begins "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.

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.

like image 2
David Aldridge Avatar answered Oct 20 '22 21:10

David Aldridge


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:

bucket.objects.with_prefix('my_folder').collect(&:key)
# => ["my_folder/myfile"]
like image 2
Featalion Avatar answered Oct 20 '22 23:10

Featalion