5

I am trying to upload an entire directory to s3 using the ruby aws-sdk gem. I am first trying to break it down in to smaller problems so what I am currently trying to do is simply create one single folder and then place a file inside of that folder. I know that technically s3 does not have folders, but objects. I do know that you can have a directory-like structure though. I can not find anything on how to do this online and the docs don't mention a lot about directory structure besides reading with AWS::S3::Tree

This is my current attempt at creating a folder and then adding a file to the folder:

#created an object called test
obj = bucket.objects.create('test', 'data')

#this is the path to a css file that I am uploading. 
path_to_file = './directory/mobile/player.css'

#writing file to test object
obj.write(Pathname.new(path_to_file))

What this is actually doing is writing the css file to test. What I want it to do is create a css file inside a folder named test.

I am sure I am misunderstanding the way objects are related to directories. Can anyone spot where I am going wrong or point me in the correct direction?

Spencer Cooley
  • 8,471
  • 16
  • 48
  • 63
  • 1
    Looks like there's no directory structure in Amazon S3: http://serverfault.com/a/435828, http://stackoverflow.com/a/11503643/1008230 – fankt May 13 '13 at 08:12
  • 4
    I found out that you can have a directory-like structure by just creating the objects with a pathname. bucket.objects.create(folder/test/player.css). When you view the bucket as_tree they will show up as directories. – Spencer Cooley May 13 '13 at 15:43
  • @SpencerCooley Can you elaborate on the solution that you found? – Noz Jan 03 '14 at 19:09
  • He is specifying the directory where he wants the file stored on the bucket on the object creation @Noz – josethernandezc Jan 24 '14 at 15:32
  • @SpencerCooley you should answer your own question, or delete the question – John Bachir Mar 15 '14 at 18:11

1 Answers1

1

You can use the following code:

# Instantiate the S3 client with your AWS credentials
s3 = AWS::S3.new(
  :access_key_id => 'Aws_Access_Key',
  :secret_access_key => 'Aws_Secret_Key'
)

# If you want to create bucket
# bucketName: name of the bucket
bucket = s3.buckets.create('bucketName', :acl => :public_read)

# push file to s3
# bucketName: Amazon Bucket Name 
# key: The file location that you want to create for your file.
# e.g, key = "user/appName/myapp.zip"  
# File_To_Save: The location of your file. 

obj = bucket.objects['key']
obj.write(:file => file_name)
puts obj.public_url

# This key describes the directory structure for your file
Ian Vaughan
  • 20,211
  • 13
  • 59
  • 79
Rohit Raina
  • 336
  • 2
  • 7
  • Any idea how to do this with v2 of the aws-sdk? Seems like a lot has changed and it's not that simple anymore. – vich Mar 22 '15 at 22:26