Class: Presigner
- Inherits:
-
Object
- Object
- Presigner
- Defined in:
- lib/presigner.rb,
lib/presigner/version.rb
Constant Summary collapse
- DEFAULT_DURATION =
60 * 30
- VERSION =
"0.0.4"
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
Instance Method Summary collapse
- #calc_duration ⇒ Object
- #generate ⇒ Object
-
#initialize(options) ⇒ Presigner
constructor
A new instance of Presigner.
Constructor Details
#initialize(options) ⇒ Presigner
Returns a new instance of Presigner.
9 10 11 12 13 14 15 |
# File 'lib/presigner.rb', line 9 def initialize() @bucket = [:bucket] @key = [:key] @duration = [:duration] || DEFAULT_DURATION @region = [:region] || "us-east-1" @base = Time.now.to_i end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
17 18 19 |
# File 'lib/presigner.rb', line 17 def base @base end |
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
17 18 19 |
# File 'lib/presigner.rb', line 17 def bucket @bucket end |
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
17 18 19 |
# File 'lib/presigner.rb', line 17 def duration @duration end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
17 18 19 |
# File 'lib/presigner.rb', line 17 def key @key end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
17 18 19 |
# File 'lib/presigner.rb', line 17 def region @region end |
Instance Method Details
#calc_duration ⇒ Object
46 47 48 |
# File 'lib/presigner.rb', line 46 def calc_duration duration end |
#generate ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/presigner.rb', line 19 def generate s3 = AWS::S3.new(region: region) # check if specified bucket and key exist. begin target_obj = s3.buckets[bucket].objects[key] if !target_obj.exists? $stderr.puts "Specified bucket or key does not exist." exit 1 end rescue AWS::S3::Errors::Forbidden $stderr.puts "Access to the S3 object is denied. Credential or target name might be wrong" exit 1 end if !target_obj.exists? $stderr.puts "Specified bucket or key does not exist." exit 1 end presigner = AWS::S3::PresignV4.new(target_obj) expires = calc_duration url = presigner.presign(:get, expires: base + duration, secure: true) url end |