Ralph Vugts
Ralph Vugts

Reputation: 425

ACL not applying during AWS s3 folder upload (uploadDirectory)

For some reason public-read is not being applied when I'm uploading a folder to an S3 bucket. (IE, public can not access the files)

The files upload fine, but they are all set to private. Tried everything I can think of. Feels like I'm missing something basic.

Was using this guide: https://blogs.aws.amazon.com/php/post/Tx2W9JAA7RXVOXA/Syncing-Data-with-Amazon-S3

Here is my code:

    require '../vendor/autoload.php';

    use Aws\S3\S3Client;

    $client = S3Client::factory(array(  
      'version' => '2006-03-01',
      'region'  => 'ap-southeast-2',
      'credentials' => array(
        'key' => 'MYKEY',
        'secret'  => 'MYSECRET',        
      )
    ));    

    $dir = 'assets';
    $bucket = 'gittestbucket';
    $keyPrefix = 'assets';      

    $options = array(
        'params'      => array('ACL' => 'public-read'),
        'concurrency' => 20,
        'debug'       => true
    );

    $UploadAWS = $client->uploadDirectory($dir, $bucket, $keyPrefix, $options);

    var_dump($UploadAWS);

My IAM user policy (also has a group of list all buckets):

    {
        "Statement": [
            {
                "Action": "s3:*",
                "Effect": "Allow",
                "Resource": [
                    "arn:aws:s3:::gittestbucket",
                    "arn:aws:s3:::gittestbucket/*",              
                ]
            }
        ]
    }

Any help much appreciated. Cheers

Upvotes: 4

Views: 860

Answers (1)

woody
woody

Reputation: 184

I struggled with this a while back.

Try changing you upload statement to this one bellow

 $UploadAWS = $client->uploadDirectory($dir, $bucket, $keyPrefix, array(
        'concurrency' => 20,
        'debug'       => true,
        'before' => function (\Aws\Command $command) {
        $command['ACL'] = strpos($command['Key'], 'CONFIDENTIAL') === false
            ? 'public-read'
            : 'private';
        }
    ));

AWS is shocking sometimes for its documentation as it changes so much

Upvotes: 8

Related Questions