ruby/example_code/s3/s3-ruby-example-bucket-policy.rb
# Updates the AWS principal Amazon Resource Name (ARN) in an existing
#   bucket policy for an Amazon Simple Storage Service (Amazon S3) bucket.
#
# Prerequisites:
#
# - An S3 bucket.
# - A bucket policy attached to the bucket.
# - A valid AWS principal 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 new AWS principal to allow.
# @return [Boolean] true if the bucket policy was updated; otherwise, false.
# @example
#   exit 1 unless bucket_policy_aws_principal_updated?(
#     Aws::S3::Client.new(region: 'us-west-2'),
#     'doc-example-bucket',
#     'arn:aws:iam::111111111111:user/SomeOtherUser'
#   )

Updates the AWS principal Amazon Resource Name (ARN) in an existing
bucket policy for an Amazon Simple Storage Service (Amazon S3) bucket.



An S3 bucket.
A bucket policy attached to the bucket.
A valid AWS principal ARN.
Updates the AWS principal Amazon Resource Name ( ARN ) in an existing bucket policy for an Amazon Simple Storage Service ( Amazon S3 ) bucket . An S3 bucket . A bucket policy attached to the bucket . A valid AWS principal ARN .
def bucket_policy_aws_principal_updated?(
  s3_client,
  bucket_name,
  new_aws_principal
)
  bucket_policy = s3_client.get_bucket_policy(bucket: bucket_name).policy.read
  policy_json = JSON.parse(bucket_policy)
  policy_json['Statement'][0]['Principal']['AWS'] = new_aws_principal
  s3_client.put_bucket_policy(
    bucket: bucket_name,
    policy: policy_json.to_json
  )
  return true
rescue StandardError => e
  puts "Error updating bucket policy: #{e.message}"
  return false
end
def bucket_policy_aws_principal_updated? ( s3_client , bucket_name , new_aws_principal ) bucket_policy = s3_client . get_bucket_policy ( bucket : bucket_name ) . policy . read policy_json = JSON . parse ( bucket_policy ) policy_json [ 'Statement' ] [ 0 ] [ 'Principal' ] [ 'AWS' ] = new_aws_principal s3_client . put_bucket_policy ( bucket : bucket_name , policy : policy_json . to_json ) return true rescue StandardError => e puts "Error updating bucket policy: #{e.message}" return false end
Updates the AWS principal Amazon Resource Name (ARN) in an existing
bucket policy for an Amazon Simple Storage Service (Amazon S3) bucket.
Updates the AWS principal Amazon Resource Name ( ARN ) in an existing bucket policy for an Amazon Simple Storage Service ( Amazon S3 ) bucket .
true if the bucket policy was updated; otherwise, false.
true if the bucket policy was updated ; otherwise false .
Boolean
s3_client
An initialized S3 client.
An initialized S3 client .
bucket_name
The bucket's name.
The bucket ' s name .
new_aws_principal
aws_principal
The ARN of the new AWS principal to allow.
The ARN of the new AWS principal to allow .
example
exit 1 unless bucket_policy_aws_principal_updated?( Aws::S3::Client.new(region: 'us-west-2'), 'doc-example-bucket', 'arn:aws:iam::111111111111:user/SomeOtherUser' )
exit 1 unless bucket_policy_aws_principal_updated? ( Aws :: S3 :: Client . new ( region : ' us - west - 2 ' ) ' doc - example - bucket ' ' arn : aws : iam :: 111111111111 : user / SomeOtherUser ' )