How to set Amazon bucket policy using the Ruby SDK?

user1813867

I'm trying to set a bucket policy using the Ruby SDK like this:

s3 = Aws::S3::Resource.new(region:'us-east-1')
obj = s3.bucket('my-bucket-name')
policy = AWS::S3::Policy.new
policy.allow(
  :actions => [:get_object],
  :resources => [obj],
  :principals => :any)

obj.policy = policy
obj.save!

But it doesn't seem to be saving. I've looked through the ruby documentation but it's not clear how to do this. I'd also like to be able to print out the current policies for the bucket if possible.

Nabeel

Have a look at the following example. This allows you to set a policy and retrieve a policy (remember to change the bucket name and add credentials / change region if need be).

You may want to fine-grain the policy itself to restrict it to some object (folders and files are both objects). Remember that if you have an S3 layout such as my-bucket-name/folder/file and you wish to only restrict to that folder then it would be arn:aws:s3:::my-bucket-name/folder/* as the ARN.

Documentation for setting and getting policy.

require 'aws-sdk'
require 'json'

s3 = Aws::S3::Client.new(region: 'us-east-1')

policy = {
  "Version":"2012-10-17",
  "Statement":[
    {
      "Sid":"AddPerm",
      "Effect":"Allow",
      "Principal": "*",
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::my-bucket-name/*"]
    }
  ]
}

s3.put_bucket_policy({
  bucket: "my-bucket-name",
  policy: policy.to_json
})

 => #<struct Aws::EmptyStructure>

resp = s3.get_bucket_policy({
  bucket: "my-bucket-name",
})

resp.policy.read
 => "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"AddPerm\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::my-bucket-name/*\"}]}"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot set bucket policy of amazon s3

how to set certain bucket in Firebase Storage using admin SDK?

Amazon S3 bucket policy to set "Content Type" to "application/pdf" for a specific folder in a bucket

When creating an amazon s3 bucket, is it possible to set a policy to be automatically written into the bucket?

How to empty Amazon S3 Bucket using AWS Java SDK?

Amazon Bucket Policy for only mobile app access

Amazon S3 Bucket Policy Public

how to I set default server side encryption of bucket using .net sdk?

Amazon s3 returns only 1000 entries for one bucket and all for another bucket (using java sdk)?

How to get bucket policy using Airflow s3 hook

How to upload large archive to Amazon Glacier using ruby and aws-sdk?

How to set the downgrading policy in Cassandra using nodejs

Uploading a file using the Ruby SDK to Amazon S3

Upload File using PHP into Amazon S3 bucket - without using SDK

How to upload file to amazon s3 bucket using axios?

How to create a folder in an amazon S3 bucket using terraform

How to search an Amazon S3 Bucket using Wildcards?

Download folder from Amazon S3 bucket using .net SDK

Get Storage used by bucket in amazon s3 using C# SDK

Amazon S3 Bucket policy to allow user to write to the bucket but that bucket only

Exclude one Amazon S3 object from a Bucket Policy

Amazon S3 Bucket Policy to prevent download?

Amazon S3 Bucket Policy - Filter by User Agent

Amazon S3 bucket policy for referer condition on specific folder

Amazon S3 Bucket Policy & Google Docs View

Amazon S3 Bucket Policy Block User Agent

How to set S3 bucket policy to (mostly) private when object acl is public?

How should I set up my bucket policy so I can deploy to S3?

How to empty a S3 bucket using the AWS SDK for GOlang?