laravelamazon-s3laravel-8

Why is my laravel s3 bucket publicly accessible url not working?


So I am trying to upload file to my S3 bucket whose bucket policy is :

{
    "Version": "2012-10-17",
    "Id": "Policy*************", 
    "Statement": [
        {
            "Sid": "Stmt***********", 
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::instagramclone123/*"
        }
    ]
}

So I have also enabled ACLs and allowed public access for my bucket.I upload my file to s3 bucket using

$filepath = Storage::disk('s3')->put($filePath, $encoded,[
                    'visibility' => 'public',
                ]);

then i use this $filepath to generate a publicly accessible url using the line Storage::url($filePath) . So although after doing all these configurations ,my file does get uploaded in the AWS bucket but when I try to access the url using the url 'http://localhost:8000/storage/posts/1737066384dollar.jpg' , I get 403 Forbidden . I have also created symlink between my storage and public folder using php artisan storage:link. This issue started arising when I manually created a new laravel project and migrated all the functions and configurations to the new project. These issues did not arise in the earlier version. In this new version i also had to manually create api.php.


Solution

  • try this policy

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "PublicReadGetObject",
                "Effect": "Allow",
                "Principal": "*",
                "Action": [
                    "s3:GetObject"
                ],
                "Resource": [
                    "arn:aws:s3:::instagramclone123/*"
                ]
            }
        ]
    }
    
    1. Go to the “Properties” tab of your bucket
    2. Click on Edit in the “Static website hosting” and choose Enable.
    3. From there, select “Use this bucket to host a website” and enter the “Index document” and “Error document” fields.

    Once you’ve completed these steps, your S3 bucket should be publicly accessible. You can test this by visiting the bucket’s URL in your web browser.