I'm looking for a solution to update dynamically my S3 files ACLs and that it propagates instantly to Cloudfront.
So far I can update the ACL on S3 but if I set a file private it will still be accessible via Cloudfront publicly and the other way around.
I am using S3 PHP SDK to do so: Set file as public
$s3 = new S3($awsAccessKey, $awsSecretKey);
if (($acp = S3::getAccessControlPolicy($bucket, $uri)) !== false) {
$acp["acl"][] = array(
"type" => "Group", "uri" => "http://acs.amazonaws.com/groups/global/AllUsers", "permission" => "READ"
);
if (S3::setAccessControlPolicy($bucket, $uri, $acp)) {
echo "true";
}
}
Set file as private
$s3 = new S3($awsAccessKey, $awsSecretKey);
if (($acp = S3::getAccessControlPolicy($bucket, $uri)) !== false) {
foreach($acp['acl'] as $key => $val) {
if(isset($val['uri']) &&
$val['uri'] == 'http://acs.amazonaws.com/groups/global/AllUsers')
unset($acp['acl'][$key]);
}
if (S3::setAccessControlPolicy($bucket, $uri, $acp)) {
echo "true";
}
}
I have read that to update a file you have to send an invalidation request to Cloudfront: Force CloudFront distribution/file update
I haven't tried it but before I do it, I would like to know it this is the right solution. I've also read that it might take 15min to update. Isn't there any way to make it instantaneous?
Thanks!