Class: S3Sync::CLI::Url

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCmd

#execute, #has_options?, #has_prefix?, #usage

Constructor Details

#initializeUrl

Returns a new instance of Url.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/s3sync/cli.rb', line 244

def initialize
  super 'url', takes_commands: 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 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.



241
242
243
# File 'lib/s3sync/cli.rb', line 241

def method
  @method
end

#secureObject

Returns the value of attribute secure.



242
243
244
# File 'lib/s3sync/cli.rb', line 242

def secure
  @secure
end

Instance Method Details

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

Raises:



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/s3sync/cli.rb', line 286

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.bucket(bucket).object(key).public_url(opts).to_s
  else
    opts.merge!({:expires => @expires_in}) if @expires_in
    puts s3.bucket(bucket).object(key).presigned_url(method.to_sym, opts).to_s
  end
end