I am trying to run aws s3 cp command from within php code using shell exec. Following is the php code.
echo shell_exec("sudo aws s3 cp s3://<bucket>/somefolder/somefile s3://<bucket>/someotherfolder/somefile --region ap-southeast-1 --acl public-read");
The file is not getting copied and The output from echo is the following
"Unable to locate credentials Completed 1 part(s) with ... file(s) remaining"
Note1: I have already set the credentials using aws configure command
Note2: If I run the exact same command directly from terminal, it works fine.
Any idea?
To resolve this issue, make sure that your AWS credentials are correctly configured in the AWS CLI. Note: If you still receive an error when running an AWS CLI command, make sure that you're using the most recent AWS CLI version.
When you use a shared profile that specifies an AWS Identity and Access Management (IAM) role, the AWS CLI calls the AWS STS AssumeRole operation to retrieve temporary credentials. These credentials are then stored (in ~/.aws/cli/cache ).
Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/ . In the navigation pane, choose Users. Choose the name of the user whose access keys you want to create, and then choose the Security credentials tab. In the Access keys section, choose Create access key.
This worked for me:
<?php
putenv('AWS_DEFAULT_REGION=your-region');
putenv('AWS_ACCESS_KEY_ID=YOURACCESSKEYID');
putenv('AWS_SECRET_ACCESS_KEY=YOURSECRETACCESSKEY');
$output = shell_exec('aws s3 cp ./file.txt s3://MYBUCKETID/ 2>&1');
echo "<pre>$output</pre>";
?>
The AWS CLI sets credentials at ~/.aws/config, and the aws php sdk looks for them at ~/.aws/credentials.
So:
cd ~/.aws
mv config credentials
Solved what I think is the same problem for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With