GSBucket publicBucket = new GSBucket(publicBucketName);
gsService.createBucket(publicBucketName);
// Retrieve the bucket's ACL and modify it to grant public access,
// ie READ access to the ALL_USERS group.
GSAccessControlList bucketAcl = gsService.getBucketAcl(publicBucketName);
bucketAcl.grantPermission(new AllUsersGrantee(), Permission.PERMISSION_READ);
// Update the bucket's ACL. Now anyone can view the list of objects in this bucket.
publicBucket.setAcl(bucketAcl);
gsService.putBucketAcl(publicBucket);
// Now let's create an object that is public from scratch. Note that we will use the bucket's
// public ACL object created above, this works fine. Although it is possible to create an
// AccessControlList object from scratch, this is more involved as you need to set the
// ACL's Owner information which is only readily available from an existing ACL.
// Create a public object in GS. Anyone can download this object.
GSObject publicObject = new GSObject("publicObject.txt", "This object is public");
publicObject.setAcl(bucketAcl);
gsService.putObject(publicBucketName, publicObject);
// The ALL_USERS Group is particularly useful, but there are also other grantee types
// that can be used with AccessControlList. Please see Google Storage technical documentation
// for a fuller discussion of these settings.
GSAccessControlList acl = new GSAccessControlList();
// Grant access by email address. Note that this only works email address of GS members.
acl.grantPermission(new UserByEmailAddressGrantee("someone@somewhere.com"),
Permission.PERMISSION_FULL_CONTROL);
// Grant Read access by Goodle ID.
acl.grantPermission(new UserByIdGrantee("Google member's ID"),
Permission.PERMISSION_READ);
// Grant Write access to a group by domain.
acl.grantPermission(new GroupByDomainGrantee("yourdomain.com"),
Permission.PERMISSION_WRITE);
}