ruby/example_code/s3/s3-ruby-example-access-permissions.rb
# Sets the access control list (ACL) for an object in an
#   Amazon S3 bucket for public access.
#
# Prerequisites:
#
# - An Amazon S3 bucket.
# - An object in the bucket.
#
# @param s3_client [Aws::S3::Client] An initialized Amazon S3 client.
# @param bucket_name [String] The bucket's name.
# @param object_key [String] The name of the object.
# @param access_level [String] The access level for the bucket. Allowed values
#   include private, public-read, public-read-write, and authenticated-read.
# @return [Boolean] true if the ACL was set; otherwise, false.
# @example
#   exit 1 unless object_acl_set?(
#     Aws::S3::Client.new(region: 'us-west-2'),
#     'doc-example-bucket',
#     'my-file.txt',
#     'private'
#   )

Sets the access control list (ACL) for an object in an
Amazon S3 bucket for public access.



An Amazon S3 bucket.
An object in the bucket.
Sets the access control list ( ACL ) for an object in an Amazon S3 bucket for public access . An Amazon S3 bucket . An object in the bucket .
def object_acl_set?(s3_client, bucket_name, object_key, access_level)
  s3_client.put_object_acl(
    bucket: bucket_name,
    key: object_key,
    acl: access_level
  )
  return true
rescue StandardError => e
  puts "Error setting object ACL: #{e.message}"
  return false
end
def object_acl_set? ( s3_client , bucket_name , object_key , access_level ) s3_client . put_object_acl ( bucket : bucket_name , key : object_key , acl : access_level ) return true rescue StandardError => e puts "Error setting object ACL: #{e.message}" return false end
Sets the access control list (ACL) for an object in an
Amazon S3 bucket for public access.
Sets the access control list ( ACL ) for an object in an Amazon S3 bucket for public access .
true if the ACL was set; otherwise, false.
true if the ACL was set ; otherwise false .
Boolean
s3_client
An initialized Amazon S3 client.
An initialized Amazon S3 client .
bucket_name
The bucket's name.
The bucket ' s name .
object_key
The name of the object.
The name of the object .
access_level
The access level for the bucket. Allowed values include private, public-read, public-read-write, and authenticated-read.
The access level for the bucket . Allowed values include private public - read public - read - write and authenticated - read .
example
exit 1 unless object_acl_set?( Aws::S3::Client.new(region: 'us-west-2'), 'doc-example-bucket', 'my-file.txt', 'private' )
exit 1 unless object_acl_set? ( Aws :: S3 :: Client . new ( region : ' us - west - 2 ' ) ' doc - example - bucket ' ' my - file . txt ' ' private ' )