Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a directory structure using the ruby aws-sdk on s3?

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?

like image 345
Spencer Cooley Avatar asked Nov 13 '22 05:11

Spencer Cooley


1 Answers

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
like image 123
Rohit Raina Avatar answered Nov 15 '22 06:11

Rohit Raina