19

I'm having a hell of a time working with the aws-sdk documentation, all of the links I follow seem outdated and unusable.

I'm looking for a straight forward implementation example of uploading an image file to an S3 bucket in Ruby.

  • let's say the image path is screenshots/image.png
  • and I want to upload it to the bucket my_bucket
  • AWS creds live in my ENV

Any advice is much appreciated.

YoDK
  • 447
  • 1
  • 4
  • 13
  • when I follow those examples I get errors like: ```undefined method `write' for # (NoMethodError)``` – YoDK Feb 17 '15 at 21:03
  • I ended up using this answer (http://stackoverflow.com/questions/130948/ruby-convert-file-to-string) then used ```object = bucket.object('image.png')``` ```object.put(body: contents)``` – YoDK Feb 17 '15 at 21:13
  • @EldadMor You linked to the v1 documentation. The v2 documentation is found here: http://docs.aws.amazon.com/sdkforruby/api/index.html – Trevor Rowe Feb 20 '15 at 23:53

1 Answers1

38

Here is how you can upload a file from disk to the named bucket and key:

s3 = Aws::S3::Resource.new
s3.bucket('my_bucket').object('key').upload_file('screenshots/image.png')

That is the simplest method. You should replace 'key' with the key you want it stored with in Amazon S3. This will automatically upload large files for you using the multipart upload APIs and will retry failed parts.

If you prefer to upload always using PUT object, you can call #put or use an Aws::S3::Client:

# using put
s3 = Aws::S3::Resource.new
File.open('screenshots/image.png', 'rb') do |file|
  s3.bucket('my_bucket').object('key').put(body:file)
end

# using a client
s3 = Aws::S3::Client.new
File.open('screenshots/image.png', 'rb') do |file|
  s3.put_object(bucket:'my_bucket', key:'key', body:file)
end

Also, the API reference documentation for the v2 SDK is here: http://docs.aws.amazon.com/sdkforruby/api/index.html

Trevor Rowe
  • 6,499
  • 2
  • 27
  • 35
  • You're my freaking hero! Spent the last 3-4 hours trying to make this work to no avail. Doing this in v1 of the aws-sdk was so simple but v2 seems unnecessarily complicated. – vich Mar 22 '15 at 23:04
  • @mmichael I'm curious in what way v2 uploads are more complicated than v1? The syntax is very similar, no? – Trevor Rowe Mar 23 '15 at 03:50
  • 1
    Well, with v1 you didn't have to deal with the Client or Resource classes, you could just run `s3 = AWS::S3.new` and then `s3.buckets['bucket_name'].objects['key'].write(file: '/path/to/file')`. I actually misspoke, the v2 version isn't more complicated. I meant to say that the v2 docs are just more confusing. Nowhere in the new docs does it show you how to do the steps in your answer. And if they do, then it's not as clear as the [instructions in v1](http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3.html) where it shows you everything you need on one page. – vich Mar 23 '15 at 05:12
  • I agree, they have raised the knowledge level above beginner in the v2 documentation – Conor Mar 25 '15 at 23:45
  • yeah, the docs are a nightmare. would be great to have a better overview of common use cases for things like Client and Resource – Brent Sep 02 '15 at 22:19