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
undefined method `write' for #<Aws::S3::Object bucket_name="my_bucket", key="image.png"> (NoMethodError)
– YoDKobject = bucket.object('image.png')
object.put(body: contents)
– YoDK