ruby/example_code/s3/s3-ruby-example-bucket-policy.rb
# Adds a bucket policy to an Amazon Simple Storage Service (Amazon S3) bucket.
#
# Prerequisites:
#
# - An S3 bucket.
# - A valid AWS principal Amazon Resource Name (ARN).
#
# @param s3_client [Aws::S3::Client] An initialized S3 client.
# @param bucket_name [String] The bucket's name.
# @param aws_principal [String] The ARN of the AWS principal to allow.
# @param action [String] The bucket action to allow.
# @return [Boolean] true if the bucket policy was added; otherwise, false.
# @example
#   exit 1 unless bucket_policy_added?(
#     Aws::S3::Client.new(region: 'us-west-2'),
#     'doc-example-bucket',
#     'arn:aws:iam::111111111111:user/SomeUser',
#     's3:GetObject'
#   )

Adds a bucket policy to an Amazon Simple Storage Service (Amazon S3) bucket.
Prerequisites.

An S3 bucket.
A valid AWS principal Amazon Resource Name (ARN).
Adds a bucket policy to an Amazon Simple Storage Service ( Amazon S3 ) bucket . Prerequisites . An S3 bucket . A valid AWS principal Amazon Resource Name ( ARN ) .
def bucket_policy_added?(s3_client, bucket_name, aws_principal, action)
  bucket_policy = {
    'Version' => '2012-10-17',
    'Statement' => [
      {
        'Effect' => 'Allow',
        'Principal' => {
          'AWS' => aws_principal
        },
        'Action' => action,
        'Resource' => "arn:aws:s3:::#{bucket_name}/*"
      }
    ]
  }.to_json
  s3_client.put_bucket_policy(
    bucket: bucket_name,
    policy: bucket_policy
  )
  return true
rescue StandardError => e
  puts "Error adding bucket policy: #{e.message}"
  return false
end
def bucket_policy_added? ( s3_client , bucket_name , aws_principal , action ) bucket_policy = { 'Version' => '2012-10-17' , 'Statement' => [ { 'Effect' => 'Allow' , 'Principal' => { 'AWS' => aws_principal } , 'Action' => action , 'Resource' => "arn:aws:s3:::#{bucket_name}/*" } ] } . to_json s3_client . put_bucket_policy ( bucket : bucket_name , policy : bucket_policy ) return true rescue StandardError => e puts "Error adding bucket policy: #{e.message}" return false end
Adds a bucket policy to an Amazon Simple Storage Service (Amazon S3) bucket.
Adds a bucket policy to an Amazon Simple Storage Service ( Amazon S3 ) bucket .
true if the bucket policy was added; otherwise, false.
true if the bucket policy was added ; otherwise false .
Boolean
s3_client
An initialized S3 client.
An initialized S3 client .
bucket_name
The bucket's name.
The bucket ' s name .
aws_principal
The ARN of the AWS principal to allow.
The ARN of the AWS principal to allow .
action
The bucket action to allow.
The bucket action to allow .
example
exit 1 unless bucket_policy_added?( Aws::S3::Client.new(region: 'us-west-2'), 'doc-example-bucket', 'arn:aws:iam::111111111111:user/SomeUser', 's3:GetObject' )
exit 1 unless bucket_policy_added? ( Aws :: S3 :: Client . new ( region : ' us - west - 2 ' ) ' doc - example - bucket ' ' arn : aws : iam :: 111111111111 : user / SomeUser ' ' s3 : GetObject ' )