2

Anyone got a working example of using ruby to post to a presigned URL on s3

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
macarthy
  • 3,074
  • 2
  • 23
  • 24

6 Answers6

2

I have used aws-sdk and right_aws both.

Here is the code to do this.

require 'rubygems'
require 'aws-sdk'
require 'right_aws'
require 'net/http'
require 'uri'
require 'rack'


access_key_id     = 'AAAAAAAAAAAAAAAAA'
secret_access_key = 'ASDFASDFAS4646ASDFSAFASDFASDFSADF'


s3 = AWS::S3.new(  :access_key_id => access_key_id, :secret_access_key => secret_access_key)

right_s3 = RightAws::S3Interface.new(access_key_id,  secret_access_key,  {:multi_thread => true, :logger => nil} ) 



bucket_name = 'your-bucket-name'
key  = "your-file-name.ext"

right_url = right_s3.put_link(bucket_name, key)
right_scan_command = "curl -I --upload-file #{key} '#{right_url.to_s}'"
system(right_scan_command)

bucket = s3.buckets[bucket_name]
form = bucket.presigned_post(:key => key)
uri = URI(form.url.to_s + '/' + key)
uri.query = Rack::Utils.build_query(form.fields)
scan_command = "curl -I --upload-file #{key} '#{uri.to_s}'"
system(scan_command)
CantGetANick
  • 1,799
  • 15
  • 25
1

I know this is an older question, but I was wondering the same thing and found an elegant solution in the AWS S3 Documentation.

require 'net/http'

file = "somefile.ext"
url = URI.parse(presigned_url)
Net::HTTP.start(url.host) do |http|
   http.send_request("PUT", url.request_uri, File.read(file), {"content-type" => "",})
end

This worked great for my Device Farm uploads.

J. Lovell
  • 31
  • 1
1

Can you provide more information on how a "presigned URL" works? Is it like this:

AWS::S3::S3Object.url_for(self.full_filename,
                          self.bucket_name, {
                            :use_ssl => true,
                            :expires_in => ttl_seconds
                          })

I use this code to send authenticated clients the URL to their S3 file. I believe this is the "presigned URL" that you're asking about. I haven't used this code for a PUT, so I'm not exactly sure if it's right for you, but it might get you close.

Dan Harper
  • 1,130
  • 10
  • 22
0

There are some generic REST libraries for Ruby; Google for "ruby rest client". See also HTTParty.

Pistos
  • 23,070
  • 14
  • 64
  • 77
0

Does anything on the s3 library page cover what you need? There are loads of examples there.

Dan Harper
  • 1,130
  • 10
  • 22
-5

I've managed to sort it out. Turns out the HTTP:Net in Ruby is has some short comings. Lot of Monkeypatch later I got it working.. More details when I have time. thank

macarthy
  • 3,074
  • 2
  • 23
  • 24