Aws-sdk-php: Credentials

Created on 24 Jan 2016  路  15Comments  路  Source: aws/aws-sdk-php

I'm using was version 3.14.1. I cannot get the credentials to give read right from the server. Even hard coding the key and secret doesn't work. I created ~/.aws/credentials.ini file that just has my key and secret in it. After that I try to call $s3Client = new S3Client([version, region, profile, etc.]). Doesn't work. Then I try the same code with my key and secret inside and that works. Please help

Most helpful comment

@dadixon "Cannot read credentials from /.aws/credentials"
Error itself saying it is looking under "/" not under home folder, so place .aws folder under "/" and give all permissions to www-data user ( chmod 700 /.aws/ ) ( chown -R www-data:www-data /.aws )

Have Fun,
@smartsiva

All 15 comments

The credentials file should be in ini format but not have a .ini extension. It should have a 'default' section defined with your key and secret:

$ less ~/.aws/credentials

[default]
aws_access_key_id = key
aws_secret_access_key = secret

Using a credentials file or environment variables is the recommended way of providing credentials on your own server, but if you prefer to hard code the key and secret, it must be passed in as part of an argument hash, not as positional parameters. For example:

$s3Client = new \Aws\S3\S3Client([
    'version' => '2006-03-01',
    'region' => $yourPreferredRegion,
    'credentials' => [
        'key' => $key,
        'secret' => $secret,
    ],
]);

Thanks. I created a new file that's just credentials with the correct format and I still get errors.

Can you share an error message or stack trace?

Cannot read credentials from /.aws/credentials

Make sure the user your code is running as has permission to read the the file. Also, make sure that you've created the file for the correct user. The SDK will attempt to read from the home folder of whatever user the code is running as.

should I open it to everyone? www-data?

nvm. that doesn't do anything either

The file will need to be in the home folder of the user running the code. If your server executes code as www-data, then on an Ubuntu server the credentials file would need to be stored as /home/www-data/.aws/credentials. The www-data user should have access to its own home folder and its contents.

I've moved the folder to my user's home folder and I still get this error. I changed the mods also to see if that was it but I get the same error. Thank you for baring with me.

Works locally. :)

Once you move the file into www-data's home folder, you should change the ownership of the file and the .aws folder to www-data as well. Call chown -R www-data:www-data /home/www-data/.aws (this may need a few tweaks depending on your linux distro). You will need to be root or have sudo privileges to do so.

I might just have to go with the hard coded way. :(

For others that find this thread...

The issue is that the sdk uses the environment variable $HOME to establish the users home directory.

You MUST set $HOME to an appropriate value before instantiating a client (otherwise the SDK looks for the credentials in root and will fail).

@dadixon "Cannot read credentials from /.aws/credentials"
Error itself saying it is looking under "/" not under home folder, so place .aws folder under "/" and give all permissions to www-data user ( chmod 700 /.aws/ ) ( chown -R www-data:www-data /.aws )

Have Fun,
@smartsiva

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Intrepidity picture Intrepidity  路  4Comments

sh-ogawa picture sh-ogawa  路  4Comments

carlalexander picture carlalexander  路  4Comments

gwilym picture gwilym  路  4Comments

chrisguitarguy picture chrisguitarguy  路  3Comments