Amazon S3, , , , , , , , " />

Creating Folders Programmatically with Amazon S3′s API: putting babies in buckets

February 5, 2009
Be careful of folder babies in buckets! photo by Patrish McLean

Be careful of folder babies in buckets! photo by Patrish McLean

Back in the ‘burgh at Carnegie Mellon’s radio station WRCT, a really awesome poster with a baby crawling to its death in a bucket reminded you of how dangerous buckets can really be. Especially when DJing techno.  I can’t work with Amazon S3 without wishing all my baby resources and folders and files would crawl in to an S3 death bucket and vanish. OMG AMAZON!

So anyhoo, to create folders instead of just files (or what appears to be folders in the awesome S3 firefox organizer), do a PUT with:

  • header content_type to binary/octet-stream
  • append _$folder$ to the end of the path name (as the extension)
  • set the data to blank

This bitty rails code demonstrates this using the S3 plugin:

  S3_FOLDER_EXT = '_$folder$'
  def exists_in_S3?(file_name, bucket_name, options ={})

    file_search_name = file_name.dup
    file_search_name << S3_FOLDER_EXT if options[:folder]

    AWS::S3::Bucket.objects(bucket_name, :prefix => file_search_name).any?


  def create_S3_folder(file_name, bucket_name, options={})
    folder = + S3_FOLDER_EXT, '', bucket_name,
      {:access => :public_read,
       :content_type => "binary/octet-stream",
       :cache_control => 'max-age=3600,post-check=900,pre-check=3600' }.update(options))

  def connect_to_S3
    unless AWS::S3::Base.connected?
      AWS::S3::Base.establish_connection!(:access_key_id => 'blah', :secret_access_key => 'giraffe!')

So your request path would look like:
And the output:

<ListBucketResult xmlns=””>

At this point, you should ask yourself why you want to create folders in the first place. Because I want to stick my head in a bucket right now.

Leave a Reply