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?
Looks like a permission/location issue of the configuration file.
Credentials set using AWS CLI, is written in a special file in the current user's path. PHP I guess is executing in other permissions (not as the same user). I would suggest you should keep the configs in a separate files and pass to the CLI this way. You also need to ensure that the specific environment variable is available inside PHP shell_exec.
To extend off of user1464317:
I found this problem exists with Yosemite vs Mavericks. The AWS CLI for Mavericks looks for permissions in "~/.aws/config", where Yosemite looks in "~/.aws/credentials".
If using cron, you can set the environment variable in crontab -e like so:
Or you do as user1464317 implied, move the file over:
In order to solve the issue, Follow the following steps:
The AWS CLI sets credentials at ~/.aws/config, and the aws php sdk looks for them at ~/.aws/credentials.
So:
Solved what I think is the same problem for me.
From your terminal, you can then run:
and then you will be asked for 4 values:
then