Flamine Flamine - 1 year ago 109
Ruby Question

Change s3 file access with aws-sdk -> 2 and ruby

I try to migrate my project to aws-sdk 2. Need to use AWS SDK for Ruby - Version 2 for this.
I found all methods, but i cant change access to file (make public).
In later version i use this:

bucket.objects[file_path].acl = :public_read

But i cant find method for changing with new api version.

This is link to old api documentation

This is link to new api documentations

Answer Source

I presume here that you want to change the object ACL after it's been uploaded to S3. If you can, consider setting the ACL when the object is sent to S3 rather than after.

There's two ways to do it. They are both similar and perform the same action. Pick the one you like best or the one you are more comfortable with.

Using the Client API

client = Aws::S3::Client.new(region: myregion)
resp = client.put_object_acl({ acl: "public-read", bucket: mybucket, key: mykey })



The Resource API

s3 = Aws::S3::Resource.new(region: myregion)
bucket = s3.bucket(mybucket)
object = bucket.object(mykey)
resp = object.acl.put({ acl: "public-read" })




If absolutely all your objects inside your bucket needs to be public, you can set the default ACL on your whole bucket so that any object uploaded will be automatically public without having you to specify it. You do that by setting a bucket policy to your bucket.

Make a bucket public in Amazon S3

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download