Class: Presigner

Inherits:
Object
  • Object
show all
Defined in:
lib/presigner.rb,
lib/presigner/version.rb

Constant Summary collapse

DEFAULT_DURATION =
60 * 30
VERSION =
"0.0.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @bucket = options[:bucket]
  @key = options[:key]
  @duration = options[:duration] || DEFAULT_DURATION
  @region = options[:region] || "us-east-1"
  @base = Time.now.to_i
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



17
18
19
# File 'lib/presigner.rb', line 17

def base
  @base
end

#bucketObject (readonly)

Returns the value of attribute bucket.



17
18
19
# File 'lib/presigner.rb', line 17

def bucket
  @bucket
end

#durationObject (readonly)

Returns the value of attribute duration.



17
18
19
# File 'lib/presigner.rb', line 17

def duration
  @duration
end

#keyObject (readonly)

Returns the value of attribute key.



17
18
19
# File 'lib/presigner.rb', line 17

def key
  @key
end

#regionObject (readonly)

Returns the value of attribute region.



17
18
19
# File 'lib/presigner.rb', line 17

def region
  @region
end

Instance Method Details

#calc_durationObject



46
47
48
# File 'lib/presigner.rb', line 46

def calc_duration
  duration
end

#generateObject



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