Wai Yan Hein
Wai Yan Hein

Reputation: 14801

Setting aws s3 file permission to public after upload in Laravel

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. enter image description here

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

Answers (3)

Jack Mackenzie
Jack Mackenzie

Reputation: 1

Setting filesystems.disks.s3.visibility to public works perfectly.

Upvotes: 0

FULL STACK DEV
FULL STACK DEV

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

Jorge Arimany
Jorge Arimany

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

Related Questions