简体   繁体   English

无法找到凭据 aws cli

[英]Unable to locate credentials aws cli

I am using AWS CLI version 2. I am using centos > Nginx > php 7.1, Following command works fine when I directly run on command line.我使用的是 AWS CLI 版本 2。我使用的是 centos > Nginx > php 7.1,当我直接在命令行上运行时,以下命令可以正常工作。

aws s3 cp files/abc.pdf s3://bucketname/

but when I run same command from index.php file using following code但是当我使用以下代码从 index.php 文件运行相同的命令时

echo exec("aws s3 cp files/abc.pdf s3://bucketname/ 2>&1");

then it gives error然后它给出错误

upload failed: Unable to locate credentials上传失败:无法找到凭据

@ Jass Add your credentials in "~/.aws/credentials" or "~/.aws/config" and make it [default] or else use profile_name incase you have multiple accounts. @ Jass在“~/.aws/credentials”或“~/.aws/config”中添加您的凭据并将其设为[默认] ,否则请使用 profile_name 以防您有多个帐户。 Also verify, if you are using keys as Environment variables by export , then it will work for that terminal only.还要验证,如果您通过export使用密钥作为环境变量,那么它仅适用于该终端。 So try to execute the php from same terminal where you exported the keys or add it in ~/.aws/credentials .因此,尝试从导出密钥的同一终端执行 php 或将其添加到~/.aws/credentials 中

I tried this and it worked for me and I believe should work for you as well.我试过这个,它对我有用,我相信也应该对你有用。 In your PHP code (index.php), try exporting the credential file location like below在您的 PHP 代码 (index.php) 中,尝试导出凭证文件位置,如下所示

echo exec("export AWS_SHARED_CREDENTIALS_FILE=/<path_to_aws_folder>/.credentials; aws s3 cp files/abc.pdf s3://bucketname/ 2>&1");

When you run from your command-line the AWS CLI picks up the credentials from your home directory ie ~/.aws/credentials (this is default).当您从命令行运行时,AWS CLI 会从您的主目录中获取凭证,即~/.aws/credentials (这是默认值)。 When the index.php is being executed it is looking for the above file in its home directory which appears is not the same as your home directory and hence cannot find the credentials.执行index.php时,它会在其主目录中查找上述文件,该文件与您的主目录不同,因此无法找到凭据。 With the above change you are explicitly pointing it to your AWS credentials.通过上述更改,您明确将其指向您的 AWS 凭证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM