I’ve been using S3 and Cloudfront from AWS to host images lately and enjoying them a lot. I use the AWS:S3 gem to access my buckets.
One of the interesting things about S3 is that there’s not really anything like a ‘folder’. A lot of GUI tools will make something that looks an awful lot like a folder, but it’s really just a key with a bunch of slashes in it.
ex: this/is/just/a/key.txt
I use Panic’s Transmit for my uploads and when I did the initial load I just copied a directory up and Transmit did all the heavy lifting, created the folder keys from the paths. No muss, no fuss. If I want to create an empty ‘folder’, I just right click and select new folder. However, if I want to do the same thing programatically, that’s a fish of a different feather. Doing some digging on the intertubes led me to a lot of people talking about how some GUIs for S3 use
I gave this a whirl, then took a look in my Transmit pane and saw new_folder_name_$folder$. Not exactly what I’d wanted. When I tried to add something like new_folder_name/file.txt I would now have 2 folders: new_folder_name and new_folder_name_$folder$. And new_folder_name wouldn’t respond properly to a get info request from Transmit.
Doing a little more digging, I found some records of people simply adding a / to the end of the key to give them their folder. When I tried this, it works like a charm:
AWS::S3::S3Object.store(“#{id}/”, “”, “bucket”, :access => :public_read);
Basically, I’m creating an empty, dynamically named folder in “bucket” with public read on it.
I dropped an image file in there to test and make sure it works and it does. So now I can create a new “folder” in my S3 bucket on every create in my app and they’re there waiting for me. It was a small annoyance, but satisfying to fix, nonetheless.
The _$folder$ trick might work for other GUIs, but this is what worked for my tool.
