2

This may apply to general file-system as well.

In my case I want to write files on Aws-S3 where the key(filename) is actually a url path.

For example :

url = "http://example.com/where/are/we/"

then filename would be

filename = "where/are/we/"

To write in S3 :

s3 = AWS::S3.new
s3.buckets[BUCKET].objects["locations/#{filename}"].write(file_contents)

But as expected the key creates sub-folders inside BUCKET/locations/where/are/we/

My last resort would be filename.gsub!('/', '_')

Any possible solution/workaround would be awesome!

swapab
  • 2,402
  • 1
  • 27
  • 44

2 Answers2

4

S3 keys are keys, not folders or filenames. But a lot of tools handle them like folders and files (what makes perfectly sense IMHO). Since everybody comes up with the analogy of folders and files, I would treat keys like folders and files. That means: Do not use slashes in keys.

Another approach might be to just add a filename that makes sense in your context. For example, if you store web-ish content in your bucket under such a key, store them under where/are/we/index.html instead of just where/are/we/.

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • Yes thats right I do store 'web-ish' content but as a JSON and I want to name the file same as the path. It seems that would break the filesystem analogy, so I `gsub('/', '_')`. – swapab Nov 21 '13 at 06:58
2

I settled with filename.gsub!('/', '_'). As we can't have slash in filename.

swapab
  • 2,402
  • 1
  • 27
  • 44