jackjoesmith
jackjoesmith

Reputation: 971

AMAZON s3- I'm able to successfully upload images to s3 bucket but can't view them

When I put in the link to the url I get this message

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>5E3C0EC6695332F6</RequestId>
<HostId>
+oAnihC+lgFxkMu5L9aUURaU/9ufCSWD0e/h4rn+LQhAp6CJox1yNJH1ojaGVvktJ0xbD7vx10=
</HostId>
</Error>

This is what my bucket policy looks like

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

I also checked off all permissions access for 'Any authenticated AWS'

update my php code

private function fillMedia($media, $user, $file, $key)
{
    if($media->category =='profile'){
        $source_file=$file->dirname .'/' . $file->basename;
    }else{
        $source_file = $file->getRealPath();
    }
    $s3 = AWS::createClient('s3');
    $result = $s3->putObject(array(
        'Bucket' => env('BUCKET_NAME'),
        'Key' => $user->id . '/'. $media->category .'/'. $key,
        'SourceFile' => $source_file,
        'Metadata' => array(
        'Owner' => $user->first_name .' ' . $user->last_name
        )
        ));
}

Upvotes: 1

Views: 2638

Answers (3)

Shyam Richard
Shyam Richard

Reputation: 1

For the above bucket policy we need to provide credentials on wp-config.php or without credential we can use the policy.

Upvotes: 0

Abrar Khan
Abrar Khan

Reputation: 175

Its works for me

$s3 = new S3("awsAccessKey", "awsSecretKey");
    $s3->putObjectFile(source, "bucket-name", destination, S3::ACL_PUBLIC_READ)); 

Upvotes: 0

user5045843
user5045843

Reputation:

I confirmed that I could access using this bucket policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::name-of-your-bucket/*"
        }
    ]
}

Here are the steps that I performed:

Step 1 - Create a S3 bucket.

Step 2 - Select the bucket and then choose Properties

Step 3 - Click "Add Bucket Policy" and insert the code snippet above. Make sure to change name-of-your-bucket to your bucket name.

Step 4 - Upload a test file to the bucket and select the file.

Step 5 - Choose Properties for the uploaded file - there will be an URL similar to https://s3-us-west-1.amazonaws.com/name-of-your-bucket/your-test-file-name.jpg

Step 6 - Use the URL above to open the test file.

When I removed the bucket policy that I used, I received the same error as you.

I hope this helps!

Upvotes: 2

Related Questions