Class: S3Ranger::CLI::Url

Inherits:
BaseCmd
  • Object
show all
Defined in:
lib/s3ranger/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCmd

#has_options?, #has_prefix?, #usage

Constructor Details

#initializeUrl

Returns a new instance of Url.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/s3ranger/cli.rb', line 218

def initialize
  super 'url', false, false

  @short_desc = "Generates public urls or authenticated endpoints for the object"
  @description = "Notice that --method and --public are mutually exclusive"
  @method = false
  @public = false
  @secure = true
  @expires_in = false
  @has_prefix = 'required'

  self.options = CmdParse::OptionParserWrapper.new do |opt|
    opt.on("-m", "--method=METHOD", "Options: #{AVAILABLE_METHODS.join ', '}") {|m|
      @method = m
    }

    opt.on("-p", "--public", "Generates a public (not authenticated) URL for the object") {|p|
      @public = p
    }

    opt.on("--no-ssl", "Generate an HTTP link, no HTTPS") {
      @secure = false
    }

    opt.on("--expires-in=EXPR", "How long the link takes to expire. Format: <# of seconds> | [#d|#h|#m|#s]") { |expr|
      val = 0
      expr.scan(/(\d+\w)/) do |track|
        _, num, what = /(\d+)(\w)/.match(track[0]).to_a
        num = num.to_i

        case what
        when "d"; val += num * 86400
        when "h"; val += num * 3600
        when "m"; val += num * 60
        when "s"; val += num
        end
      end
      @expires_in = val > 0 ? val : expr.to_i
    }
  end
end

Instance Attribute Details

#methodObject

Returns the value of attribute method.



215
216
217
# File 'lib/s3ranger/cli.rb', line 215

def method
  @method
end

#secureObject

Returns the value of attribute secure.



216
217
218
# File 'lib/s3ranger/cli.rb', line 216

def secure
  @secure
end

Instance Method Details

#run(s3, bucket, key, file, args) ⇒ Object

Raises:



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/s3ranger/cli.rb', line 260

def run s3, bucket, key, file, args
  raise WrongUsage.new(nil, "You need to inform a bucket") if not bucket
  raise WrongUsage.new(nil, "You need to inform a key") if not key
  raise WrongUsage.new(nil, "Params --method and --public are mutually exclusive") if (@method and @public)
  if not AVAILABLE_METHODS.include? method = @method || 'read'
    raise WrongUsage.new(nil, "Unknown method #{method}")
  end

  opts = {}
  opts.merge!({:secure => @secure})

  if @public
    puts s3.buckets[bucket].objects[key].public_url(opts).to_s
  else
    opts.merge!({:expires => @expires_in}) if @expires_in
    puts s3.buckets[bucket].objects[key].url_for(method.to_sym, opts).to_s
  end
end