user984621
user984621

Reputation: 48453

How to upload an image to Amazon S3 into a folder in ruby?

I am trying to do it like this:

AWS.config(
      :access_key_id => '...', 
      :secret_access_key => '...'
    )
    s3 = AWS::S3.new
    bucket_name = 'bucket_name'
key = "#{File.basename(avatar_big)}"
s3.buckets[bucket_name].objects[key].write(:file => avatar_big_path)

This working well for a file, the file is uploaded to the root of the set up bucket.

However, how to upload it into the foloder photos that is located in root?

I've tried

key = "photos/#{File.basename(avatar_big)}"

but this doesn't work.

EDIT: error message enter image description here

Thank you

Upvotes: 1

Views: 1309

Answers (2)

Miguelb
Miguelb

Reputation: 103

I had the same issue as the OP. This is what worked for me:

key = "photos/example.jpg"
bucket = s3.buckets[bucket_name]
filepath = Pathname.new("path/to/example.jpg")

o = bucket.objects[key]
o.write(filepath)

Something I would check out would be the object key you are trying to use. There's not much documentation on what are the restrictions are (see this and this) but the one shown in error message looks suspicious to me.

Upvotes: 1

Gotjosh
Gotjosh

Reputation: 1049

Try including the the path in the file key:

 s3.buckets[bucket_name].objects[key].write(:file => "photos/#{avatar_big_path}")

Upvotes: 0

Related Questions