44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/filefm/uploaders/swift.rb', line 44
def self.upload(source, destination, options = {})
uri = URI.parse(destination)
if uri.path.split("/").size == 2
uri.path = File.join(uri.path,File.basename(source))
end
if (not options[:username] or not options[:password])
raise "Invalid Credentials"
end
secure = options[:secure] == true
scheme = secure ? "https" : "http"
username = options[:username]
password = options[:password]
if options[:auth_url]
auth_url = options[:auth_url]
username,tenant = username.split(":")
out = authenticate_keystone(username, tenant, password, auth_url)
storage_url = out["X-Storage-Url"]
auth_token = out["X-Auth-Token"]
else
Log.debug "Using Swift legacy auth"
Log.debug "Legacy auth URL #{scheme}://#{uri.host}:#{uri.port}/auth/v1.0"
out = RestClient.get "#{scheme}://#{uri.host}:#{uri.port}/auth/v1.0", 'X-Storage-User' => username, 'X-Storage-Pass' => password
storage_url = out.[:x_storage_url]
auth_token = out.[:x_auth_token]
raise "Error authenticating" unless out.code == 200
end
Log.debug "authentication OK"
Log.debug "X-Storage-Url: #{storage_url}"
begin
container = "#{uri.path.split("/")[1]}"
out = RestClient.get storage_url + "/#{container}", 'X-Storage-User' => username, 'X-Auth-Token' => auth_token
rescue
raise "Error accessing the container: #{container}"
end
if options[:progressbar]
pbar = ProgressBar.new "Progress", 100
fsize = File.size(source)
count = 0
end
res = FileFM::StreamingUploader.put(
storage_url + uri.path,
:headers => { 'X-Auth-Token' => auth_token }, :file => File.open(source)
) do |size|
if block_given?
yield size
elsif options[:progressbar]
count += size
per = 0
if fsize == 0
per = 100
else
per = (100*count)/fsize rescue 100
end
per = 100 if per > 100
pbar.set per
else
end
end
if options[:progressbar]
pbar.finish
end
end
|