Skip to content

Instantly share code, notes, and snippets.

@fasl
Forked from schacon/upload.rb
Created January 29, 2018 04:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fasl/48f88a0606300cf9eb5a0d4c362e42c1 to your computer and use it in GitHub Desktop.
Save fasl/48f88a0606300cf9eb5a0d4c362e42c1 to your computer and use it in GitHub Desktop.
ruby script to upload a file to GitHub downloads section
#! /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