Reputation: 14801
I am developing a web application. In my application, I am implementing file uploaded feature. I store the uploaded file in the aws s3 bucket. I am uploading the file to the s3 bucket as follow.
$photo_file_path = $request->file('image_file')->store(
'images/artworks/gallery-images/'.uniqid(), 's3'
);
The above code is working fine. The file is uploaded to s3. But the problem is I want the file to be public when it is uploaded to s3 so that the photo can be accessible from url as well. I set both bucket policy and access control to public.
This is my bucket policy
{
"Version": "2012-10-17",
"Id": "Policy1234566788",
"Statement": [
{
"Sid": "Stmt151234344545",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::my-bucket"
}
]
}
As you can see my bucket is now public for both access control and bucket policy.
But when I upload the file from Laravel, it is uploaded to the bucket, but the file is still hidden from the public. So, how can I change the file permission to public once the file is uploaded, please?
Upvotes: 5
Views: 21297
Reputation: 1
Setting filesystems.disks.s3.visibility
to public
works perfectly.
Upvotes: 0
Reputation: 15961
Laravel provides different methods to store the files.
One of the easiest way is to use storePublicly()
:
$photo_file_path = $request->file('image_file')->storePublicly(
'images/artworks/gallery-images/'.uniqid()
);
Upvotes: 11
Reputation: 5882
if all your stored files can or should have public you can add 'visibility' => 'public'
, so any file stored in the drive will have public visibility :
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => ('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
'visibility' => 'public',
],
if not, you can configure another drive with same settings including the public flag:
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => ('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
's3Public' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => ('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
'visibility' => 'public',
],
and then just call store selecting the drive with public visibility:
$photo_file_path = $request->file('image_file')->store(
'images/artworks/gallery-images/'.uniqid(), 's3Public'
);
Upvotes: 14