Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing AWS CLI command from php results in Unable to locate credentials

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?

like image 337
Ajay Narang Avatar asked Feb 21 '14 22:02

Ajay Narang


People also ask

How can I fix the error unable to locate credentials when I try to connect to my Amazon S3 bucket using the AWS CLI?

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.

How does AWS CLI get credentials?

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 ).

How do I add AWS credentials to terminal?

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.


2 Answers

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>";

?>

like image 185
spcsLrg Avatar answered Sep 16 '22 15:09

spcsLrg


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.

like image 43
user1464317 Avatar answered Sep 19 '22 15:09

user1464317