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

后端 未结 6 1354
太阳男子
太阳男子 2020-12-11 15:56

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:///s         


        
相关标签:
6条回答
  • 2020-12-11 16:23

    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:

    # Mavericks
    AWS_CONFIG_FILE="/Users/YOUR_USER/.aws/config"
    
    # Yosemite
    AWS_CONFIG_FILE="/Users/YOUR_USER/.aws/credentials"
    

    Or you do as user1464317 implied, move the file over:

    mv config credentials
    
    0 讨论(0)
  • 2020-12-11 16:25

    From your terminal, you can then run:

    $ cat ~/.aws/config
    [profile eb-cli]
    aws_access_key_id = XXXXXX
    aws_secret_access_key = XXXXXX
    
    $ aws configure
    

    and then you will be asked for 4 values:

    1. Access Key ID (Access Key) - This is the shorter of the two values.
    2. Secret Access Key
    3. Default region name (type in 'us-east-1')
    4. Output format (type in 'json')

    then

    $ aws s3 sync s3://<bucketname>
    
    0 讨论(0)
  • 2020-12-11 16:31

    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.

    0 讨论(0)
  • 2020-12-11 16:40

    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.

    0 讨论(0)
  • 2020-12-11 16:41

    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>";
    
    ?>
    
    
    0 讨论(0)
  • 2020-12-11 16:43

    In order to solve the issue, Follow the following steps:

    1. copy .aws directory from /home/non-rootUser/.aws to /var/www
    2. Now change the directory to /var/www (cd /var/www)
    3. Change the permissions: /www$ sudo chmod -R 755 .aws
    4. Change Ownership /www$ sudo chown -R $USER:$USER .aws
    5. Recheck
    0 讨论(0)
提交回复
热议问题