Created
April 16, 2012 23:48
-
-
Save schacon/2402396 to your computer and use it in GitHub Desktop.
ruby script to upload a file to GitHub downloads section
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# | |
# ruby upload.rb user pass user/repo file '(description)' | |
# | |
require 'json' | |
if ARGV.size < 4 | |
puts "\nUSAGE: upload.rb [user] [pass] [user/repo] [filepath] ('description')" | |
exit | |
end | |
user = ARGV[0] | |
pass = ARGV[1] | |
repo = ARGV[2] | |
file = ARGV[3] | |
desc = ARGV[4] rescue '' | |
def url(path) | |
"https://api.github.com#{path}" | |
end | |
size = File.size(file) | |
fname = File.basename(file) | |
# create entry | |
args = | |
data = `curl -s -XPOST -d '{"name":"#{fname}","size":#{size},"description":"#{desc}"}' -u "#{user}:#{pass}" #{url("/repos/#{repo}/downloads")}` | |
data = JSON.parse(data) | |
# upload file to bucket | |
cmd = "curl -s " | |
cmd += "-F \"key=#{data['path']}\" " | |
cmd += "-F \"acl=#{data['acl']}\" " | |
cmd += "-F \"success_action_status=201\" " | |
cmd += "-F \"Filename=#{data['name']}\" " | |
cmd += "-F \"AWSAccessKeyId=#{data['accesskeyid']}\" " | |
cmd += "-F \"Policy=#{data['policy']}\" " | |
cmd += "-F \"Signature=#{data['signature']}\" " | |
cmd += "-F \"Content-Type=#{data['mime_type']}\" " | |
cmd += "-F \"file=@#{file}\" " | |
cmd += "https://github.s3.amazonaws.com/" | |
xml = `#{cmd}` | |
if m = /\<Location\>(.*)\<\/Location\>/.match(xml) | |
puts "Your file is uploaded to:" | |
puts m[1].gsub('%2F', '/') # not sure i want to fully URL decode this, but these will not do | |
else | |
puts "Upload failed. Response is:\n\n #{xml}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment