Ruby Fog gem: how to create sub-directories?
Asked Answered
C

1

7

I have

  connection = Fog::Storage.new(fog_config)
  bucket     = connection.directories.get(bucket_name)

Is there a way (documented, non-documented, work-around) for me to create directories inside of this bucket? Something like:

sub_dir_for_user_1 = bucket.create_sub_dir('/user_1_files')
sub_dir_for_user_2 = bucket.create_sub_dir('/user_2_files')
Caskey answered 30/7, 2015 at 19:50 Comment(2)
AFAIK Amazon S3 doesn't have the concept of directories, it's a flat file system. Directories are only presented in the browser UI for convenience.Enzymolysis
Yeah, there are no true directories in S3, just long paths (where in some contexts prefixes delineated by slashes are treated kind-of-like subdirectories).Dollop
P
8

In S3 zero byte files with a trailing slash will create a pseudo-directory. Which will cause folders to appear in the AWS Browser UI.

For fog passing nil into the body argument creates an empty file. So the following code would create a subdirectory...

bucket.files.create(
  key: 'user_1_files/',
  body: nil
)
Pertain answered 13/9, 2016 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.