module AwsExtensions::S3::BucketPolicy

Public Instance Methods

policy_hash() click to toggle source

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

# 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_string() click to toggle source

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

Returns the policy as a string.

# 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