× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Uploading an image to S3 using aws-sdk v2

Aug 08, 2016
<p>Here is how you can upload a file from disk to the named bucket and key:</p> <pre><code class="language-ruby">s3 = Aws::S3::Resource.new s3.bucket('my_bucket').object('key').upload_file('screenshots/image.png')</code></pre> <p>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.</p> <p>If you prefer to upload always using PUT object, you can call <code>#put</code> or use an <code>Aws::S3::Client</code>:</p> <pre><code class="language-ruby"># 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</code></pre> <pre><code class="language-ruby"># 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</code></pre> <p>Also, the API reference documentation for the v2 SDK is here: <a href="http://docs.aws.amazon.com/sdkforruby/api/index.html">http://docs.aws.amazon.com/sdkforruby/api/index.html</a> </p>
comments powered by Disqus