ruby/example_code/s3/s3-ruby-example-access-permissions.rb
def run_me
  bucket_name = 'doc-example-bucket'
  object_key = 'my-file.txt'
  object_content = 'This is the content of my-file.txt.'
  access_level_before = 'private'
  access_level_after = 'public-read'
  region = 'us-west-2'
  s3_client = Aws::S3::Client.new(region: region)

  # Set the initial access level of the bucket to 'private'
  #   for public access.
  if bucket_acl_set?(s3_client, bucket_name, access_level_before)
    puts "1. Initial bucket ACL set to '#{access_level_before}' " \
      "for public access.\n\n"
  else
    puts "1. Initial bucket ACL not set to '#{access_level_before}' " \
      'for public access. Stopping program.'
    exit 1
  end

  # What happens when you try to access the bucket? (It should be denied.)
  puts "2. After initial bucket ACL set to '#{access_level_before}' " \
    "for public access, trying to access the bucket:\n\n"
  object_content_by_bucket_unsigned_request(bucket_name, region)

  # Upload an object to the bucket.
  if object_uploaded?(s3_client, bucket_name, object_key, object_content)
    puts "3. Object uploaded to bucket.\n\n"
  else
    puts '3. Object not uploaded to bucket. Stopping program. ' \
      "Note that the bucket ACL is still set to '#{access_level_before}' " \
      'for public access.'
    exit 1
  end

  # What happens when you try to access the object now?
  #   (It should still be denied.)
  puts "4. After object uploaded, trying to access the object:\n\n"
  object_content_by_object_unsigned_request(bucket_name, object_key, region)

  # Now set the initial access level of the object to 'public-read'
  #   for public access.
  if object_acl_set?(s3_client, bucket_name, object_key, access_level_after)
    puts "5. Object ACL set to '#{access_level_after}' for public access.\n\n"
  else
    puts "5. Object ACL not set to '#{access_level_after}' for public " \
      'access. Stopping program. ' \
      "Note that the bucket ACL is still set to '#{access_level_before}' " \
      'for public access.'
    exit 1
  end

  # What happens when you try to access the object now? (It should now work.)
  puts "6. After object ACL set to '#{access_level_after}' for public " \
    "access, trying to access the object:\n\n"
  object_content_by_object_unsigned_request(bucket_name, object_key, region)

  # Now set the access level for the object to 'private' for public access.
  if object_acl_set?(s3_client, bucket_name, object_key, access_level_before)
    puts "7. Object ACL now set to '#{access_level_before}' " \
      "for public access.\n\n"
  else
    puts "7. Object ACL not set to '#{access_level_before}' " \
      'for public access. Stopping program. ' \
      "Note that the bucket ACL is still set to '#{access_level_before}'."
    exit 1
  end

  # What happens when you try to access the object now?
  #   (It should now be denied.)
  puts "8. After object ACL set to '#{access_level_before}' " \
    "for public access, trying to access the object:\n\n"
  object_content_by_object_unsigned_request(bucket_name, object_key, region)

  puts '9. Program ends. Note that the bucket ACL is still set to ' \
    "'#{access_level_before}' for public access."
end
# Full example call:
# Replace us-west-2 with the AWS Region you're using for Amazon S3.

Full example call:
Replace us-west-2 with the AWS Region you're using for Amazon S3.
Full example call : Replace us - west - 2 with the AWS Region you ' re using for Amazon S3 .
def run_me
  bucket_name = 'doc-example-bucket'
  object_key = 'my-file.txt'
  object_content = 'This is the content of my-file.txt.'
  access_level_before = 'private'
  access_level_after = 'public-read'
  region = 'us-west-2'
  s3_client = Aws::S3::Client.new(region: region)
  if bucket_acl_set?(s3_client, bucket_name, access_level_before)
    puts "1. Initial bucket ACL set to '#{access_level_before}' " \
      "for public access.\n\n"
  else
    puts "1. Initial bucket ACL not set to '#{access_level_before}' " \
      'for public access. Stopping program.'
    exit 1
  end
  puts "2. After initial bucket ACL set to '#{access_level_before}' " \
    "for public access, trying to access the bucket:\n\n"
  object_content_by_bucket_unsigned_request(bucket_name, region)
  if object_uploaded?(s3_client, bucket_name, object_key, object_content)
    puts "3. Object uploaded to bucket.\n\n"
  else
    puts '3. Object not uploaded to bucket. Stopping program. ' \
      "Note that the bucket ACL is still set to '#{access_level_before}' " \
      'for public access.'
    exit 1
  end
  puts "4. After object uploaded, trying to access the object:\n\n"
  object_content_by_object_unsigned_request(bucket_name, object_key, region)
  if object_acl_set?(s3_client, bucket_name, object_key, access_level_after)
    puts "5. Object ACL set to '#{access_level_after}' for public access.\n\n"
  else
    puts "5. Object ACL not set to '#{access_level_after}' for public " \
      'access. Stopping program. ' \
      "Note that the bucket ACL is still set to '#{access_level_before}' " \
      'for public access.'
    exit 1
  end
  puts "6. After object ACL set to '#{access_level_after}' for public " \
    "access, trying to access the object:\n\n"
  object_content_by_object_unsigned_request(bucket_name, object_key, region)
  if object_acl_set?(s3_client, bucket_name, object_key, access_level_before)
    puts "7. Object ACL now set to '#{access_level_before}' " \
      "for public access.\n\n"
  else
    puts "7. Object ACL not set to '#{access_level_before}' " \
      'for public access. Stopping program. ' \
      "Note that the bucket ACL is still set to '#{access_level_before}'."
    exit 1
  end
  puts "8. After object ACL set to '#{access_level_before}' " \
    "for public access, trying to access the object:\n\n"
  object_content_by_object_unsigned_request(bucket_name, object_key, region)
  puts '9. Program ends. Note that the bucket ACL is still set to ' \
    "'#{access_level_before}' for public access."
end
def run_me bucket_name = 'doc-example-bucket' object_key = 'my-file.txt' object_content = 'This is the content of my-file.txt.' access_level_before = 'private' access_level_after = 'public-read' region = 'us-west-2' s3_client = Aws :: S3 :: Client . new ( region : region ) if bucket_acl_set? ( s3_client , bucket_name , access_level_before ) puts "1. Initial bucket ACL set to '#{access_level_before}' " "for public access.\n\n" else puts "1. Initial bucket ACL not set to '#{access_level_before}' " 'for public access. Stopping program.' exit 1 end puts "2. After initial bucket ACL set to '#{access_level_before}' " "for public access, trying to access the bucket:\n\n" object_content_by_bucket_unsigned_request ( bucket_name , region ) if object_uploaded? ( s3_client , bucket_name , object_key , object_content ) puts "3. Object uploaded to bucket.\n\n" else puts '3. Object not uploaded to bucket. Stopping program. ' "Note that the bucket ACL is still set to '#{access_level_before}' " 'for public access.' exit 1 end puts "4. After object uploaded, trying to access the object:\n\n" object_content_by_object_unsigned_request ( bucket_name , object_key , region ) if object_acl_set? ( s3_client , bucket_name , object_key , access_level_after ) puts "5. Object ACL set to '#{access_level_after}' for public access.\n\n" else puts "5. Object ACL not set to '#{access_level_after}' for public " 'access. Stopping program. ' "Note that the bucket ACL is still set to '#{access_level_before}' " 'for public access.' exit 1 end puts "6. After object ACL set to '#{access_level_after}' for public " "access, trying to access the object:\n\n" object_content_by_object_unsigned_request ( bucket_name , object_key , region ) if object_acl_set? ( s3_client , bucket_name , object_key , access_level_before ) puts "7. Object ACL now set to '#{access_level_before}' " "for public access.\n\n" else puts "7. Object ACL not set to '#{access_level_before}' " 'for public access. Stopping program. ' "Note that the bucket ACL is still set to '#{access_level_before}'." exit 1 end puts "8. After object ACL set to '#{access_level_before}' " "for public access, trying to access the object:\n\n" object_content_by_object_unsigned_request ( bucket_name , object_key , region ) puts '9. Program ends. Note that the bucket ACL is still set to ' "'#{access_level_before}' for public access." end
Full example call:
Replace us-west-2 with the AWS Region you're using for Amazon S3.
Full example call : Replace us - west - 2 with the AWS Region you ' re using for Amazon S3 .
# Set the initial access level of the bucket to 'private'
 #   for public access.
 # What happens when you try to access the bucket? (It should be denied.)
 # Upload an object to the bucket.
 # What happens when you try to access the object now?
 #   (It should still be denied.)
 # Now set the initial access level of the object to 'public-read'
 #   for public access.
 # What happens when you try to access the object now? (It should now work.)
 # Now set the access level for the object to 'private' for public access.
 # What happens when you try to access the object now?
 #   (It should now be denied.)
