Flamine Flamine - 2 days ago 5
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

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 })

Documentation:

http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Client.html#put_object_acl-instance_method


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" })

Documentation:

http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/ObjectAcl.html#put-instance_method


Bonus

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

Comments