1
votes

I have the exact same issue as Executing AWS CLI command from php results in Unable to locate credentials

I did:

cd ~/.aws
mv config credentials

But it didn't help.

So what I did is, both config and credentials have the same content:

[default]

aws_access_key_id = AKIAJIPLT472C32RD6AQ

aws_secret_access_key = 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

output = json

region = ap-southeast-1

The code I'm using in my PHP file is:

$s3 = system("aws s3 cp /var/www/html/v2/upload/__rmx4hqf.png s3://<bucket>/somefolder 2>&1");
3
remove 2>&1 from the end to get the error.Jigar
@Jigar when i remove this line error is not showing, but file is still not being uploadedwoshitom
you should get the error in $s3 so var_dump($s3)Jigar

3 Answers

0
votes

My guess is that php command cannot locate aws credential file because the user you ran $ aws configure is different from php's effective user. You didn't supply enough information about how you ran configure command or how php script is executed, so it's just my guess.

What happens if you explicitly pass credential file's path via environment variable AWS_CONFIG_FILE (e.g. $s3 = system("AWS_CONFIG_FILE=/path/to/credential aws s3 cp ...)?

0
votes

Try adding your region

$s3 = system("aws s3 cp /var/www/html/v2/upload/__rmx4hqf.png s3://<bucket>/somefolder 2>&1 --region ap-southeast-1");
0
votes

like quiver say apache2 run from diffrent user then termina user you can see apache user ENV vars with the command getenv(); to add vars to apache2 ENV you need to use putenv("string");

you need to add aws cerdentials in this way

putenv('AWS_DEFAULT_REGION=' . $region); putenv('AWS_ACCESS_KEY_ID=' . $key); putenv('AWS_SECRET_ACCESS_KEY=' . $secret);e print_r(getenv());

only this solution wotks for me when i try to run aws cli from php file

original answer aws forum