I’m running a website using laravel (v.10), filament (v.3) and I’m using SpatieMediaLibrary (updated to latest version) for uploading files to aws. I serve the content through CloudFront.
My bucket policy for the moment is:
{
"Version": "2012-10-17",
"Id": "PolicyForCloudFrontPrivateContent",
"Statement": [
{
"Sid": "AllowCloudFrontServicePrincipal",
"Effect": "Allow",
"Principal": {
"Service": "cloudfront.amazonaws.com"
},
"Action": [
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::xxxxxx.xxx/*",
"Condition": {
"StringEquals": {
"AWS:SourceArn": "arn:aws:cloudfront::xxxxx:distribution/xxxxxx"
}
}
}
]}
My config/filesystems.php (please notice the variable AWS_S3_CLOUDFRONT):
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'throw' => false,
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
'throw' => false,
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'folder' => env('AWS_FOLDER'),
'view_url' => env('AWS_VIEW_URL'),
'url' => env('AWS_S3_CLOUDFRONT'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'throw' => true,
],
Before using cloudfront whenever i updated or deleted the files they were deleted from the bucket. But since I moved to serving with cloudfront, I’m able to create but not delete/replace. It’s not a cache issue, I’m confirming this directly on the bucket, logged as root (login via browser https://xxx.console.aws.amazon.com/s3/buckets) .
There are no errors on laravel logs, it still deletes/updates on the database but not in filesystem.
For clarification, I’m deleting using the SpatieMediaLibraryFileUpload component.
I’ve also tested with "Principal":"*" but the file still remained .
I’m kind of new in cloudfront, what am I missing here?
desired behavior: when updating/deleting the files using the package component (SpatieMediaLibraryFileUpload) the file should also be updated/deleted in the filesystem.
specific problem or error: Not sure here due to lack of logs, but my understanding is that it’s due to the usage of cloudfront
2
Answers
Found the issue.
It was a problem with my CustomPathGenerator class for the media package, namely on the getPath function:
CloudFront caches files in each edge location. Here’s how it works:
If you want to stop a file from being served from the cache, you need to invalidate them from the CloudFront cache. This will remove them from all edge location caches, causing CloudFront to re-fetch them from the origin, or give an error if the file no longe exists.
See: Invalidate files to remove content – Amazon CloudFront