Module: AwsExtensions::S3::BucketPolicy

Defined in:
lib/aws_extensions/s3/BucketPolicy.rb

Instance Method Summary collapse

Instance Method Details

#policy_hashObject

Public: Method returns the bucket policy as a sorted hash if no policy exists, returns nil



27
28
29
30
31
32
33
# File 'lib/aws_extensions/s3/BucketPolicy.rb', line 27

def policy_hash
  # rescue and ignore all excpetions related to no policy existing
  # We have to do this because catching an exception is the ONLY way to determine if there is a policy.
  JSON.parse(policy.string).deep_sort
rescue Aws::S3::Errors::NoSuchBucketPolicy
  nil
end

#policy_stringObject

Public: Method that will either return the bucket policy, or an empty string if there is no policy.

Returns the policy as a string.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/aws_extensions/s3/BucketPolicy.rb', line 12

def policy_string
  # fetch the sorted policy
  hash = policy_hash
  # check if policy exists
  unless hash.nil?
    # convert the policy to string
    JSON.generate(hash)
  else
    # if no policy exists, return an empty string
    ""
  end
end