socketry / multipart-post

Adds multipart POST capability to net/http
MIT License
293 stars 72 forks source link

support: How to us to upload to S3 (github file upload) #17

Closed stepheneb closed 12 years ago

stepheneb commented 12 years ago

Hi Nick,

This really just a support request (don't think it is a bug) ... if you have time to answer or sample code to point me to.

I'm working on making a script to take my gh-pages branch and upload itto github

They use a two part POST technique described here: http://developer.github.com/v3/repos/downloads/

Here's some of my code -- the first POST using HTTParty works and I get back valid json.

class Github
  include HTTParty
  base_uri 'https://api.github.com/repos/concord-consortium/lab'
  format :json
  basic_auth CONFIG[:username], CONFIG[:password]
  debug_output
end

tag = `git log -1 --date=short --format=%cd-%h gh-pages`.strip
name = "lab-dist-#{tag}.tar.gz"
if File.exist?("uploads/#{name}") || system("git archive gh-pages | gzip >uploads/#{name}")
  options = {
    "name" => name,
    "size" => File.stat("uploads/#{name}").size,
    "description" => "Latest release of Lab distribution",
    "content_type" => "application/x-gzip"
  }
  response = Github.post('/downloads', :body => MultiJson.encode(options))
  pp response

This is the part that doesn't work:

url = URI.parse(response['s3_url'])
File.open("#{PROJECT_ROOT}/uploads/#{name}") do |archive|
  req = Net::HTTP::Post::Multipart.new url.path,
    'key'            => response['path'],
    'acl'            => response['acl'],
    'success_action_status' => 201,
    'Filename'       => response['name'],
    'AWSAccessKeyId' => response['accesskeyid'],
    'Policy'         => response['policy'],
    'Signature'      => response['signature'],
    'Content-Type'   => response['mime_type'],
    "file"           => UploadIO.new(archive, response['mime_type'])
  res = Net::HTTP.start(url.host, url.port) do |http|
    http.request(req)
  end
end

I'm pretty sure thats NOT how to add the extra parameters for the multipart post -- but I didn't see an example combining the file and the other form' elements.

Thanks for any help!

nicksieger commented 12 years ago

No, that's actually exactly how you do it. What error(s) are you seeing?

stepheneb commented 12 years ago

I've put the code and response in the console here: https://gist.github.com/2198834

nicksieger commented 12 years ago

Curious, did you ever get this to work? I can't figure out what the issue is here. The error is confusing, because on line 59 you are passing 2 arguments even though it's claiming you're only passing 1.

stepheneb commented 12 years ago

I haven't got back to it yet. Probably will in the next two weeks. I did get this response from github support. When I add uploading project artifact scripts to our work I'll take a look at this suggestion and perhaps my original code. If I find out what was wrong with my earlier script I'll update this thread.

From: Rick (GitHub Staff) Subject: Ruby example for uploading to repo downloads

Sure, this is using my Faraday gem (0.8)

https://raw.github.com/gist/db607c200634abfa7a06/ae1e0cc60e4188d0a408114bfe80b473a78ad4e7/gistfile1.rb

srogers commented 11 years ago

It would be helpful if there were something in the README about non-file params. Like stepheneb said above - I was pretty sure that's not how to add more parameters into the multipart post. I spent a lot of time browsing the methods trying to figure it out - but yes, it's just that easy.