Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define permissions in Amazon s3

Tags:

amazon-s3

I have a structure on amazon like this -

(bucket name) MyImages
  ---  (key) general
       ---- 1.jpg
       ---- 2.jpg

I have created the key (general) by using S3 Firefox Organizer tool and set read permission for all. Now, by a java program when I am uploading the images inside this key, I want to set the permission of each object as the key have. But its not happening and I have to write some extra line of code for setting up the permissions of each object.

AccessControlList acl = s3.getBucketAcl("MyImages");
// give everyone read access
acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
s3.setObjectAcl("MyImages", "general/1.jpg", acl);

Is there any way to get rid of above code. Why the objects are not getting the permission as the key or bucket?

like image 997
Saurabh Avatar asked Aug 04 '10 17:08

Saurabh


1 Answers

The following code works for me. Replace file_to_save with the file object you are writing to S3.

PutObjectRequest por = new PutObjectRequest("MyImages", "general/1.jpg", file_to_save);

o.setCannedAcl(CannedAccessControlList.PublicRead);
like image 194
jdchowell Avatar answered Oct 06 '22 01:10

jdchowell