Question

Is there a way to grant a specific IAM user or group full permissions on an AWS S3 file through the AWS PHP SDK?

I've tried the following commands:

// Grant permissions via user id
$s3->putObjectAcl(array(
    'Bucket'       => '[Bucket name]',
    'Key'          => '[File location in bucket]',
    'Grants' => array(
        array(
            'Grantee' => array(
                'ID' => '[User ID from IAM API]',
                'Type' => 'CanonicalUser'
            ),
            'Permission' => 'FULL_CONTROL'
        )
    )
));

// Grant permissions via group id
$s3->putObjectAcl(array(
    'Bucket'       => '[Bucket name]',
    'Key'          => '[File location in bucket]',
    'Grants' => array(
        array(
            'Grantee' => array(
                'ID' => '[Group ID from IAM API]',
                'Type' => 'Group'
            ),
            'Permission' => 'FULL_CONTROL'
        )
    )
));

But they always throw the following error:

 Argument format not recognized

Can someone tell me what I'm doing wrong?

Any help would be greatly appreciated.

Was it helpful?

Solution

In the above code you didn't specify the SourceFile, so its throwing the error. Here's the updated code, that will help you solve this problem.

// Grant permissions via user id
 $s3->putObjectAcl(array(
      'Bucket'       => '[Bucket name]',
      'Key'          => '[File location in bucket]',
      'SourceFile'   => '[File to be uploaded location]',   // add this line
      'Grants' => array(
         array(
             'Grantee' => array(
             'ID' => '[User ID from IAM API]',
             'Type' => 'CanonicalUser'
         ),
             'Permission' => 'FULL_CONTROL'
      )
    )
 ));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top