简体   繁体   English

我无法使用 CGI-Python 脚本加载 AWS 凭证

[英]I am not able to load AWS credentials using CGI-Python script

not able to access AWS credentials when I execute python CGI script in browser.在浏览器中执行 python CGI 脚本时无法访问 AWS 凭证。

dynamodb = boto3.resource('dynamodb') By using above line in python file, I can able to get credentials from ~/.aws folder and execute it in CONSOLE. dynamodb = boto3.resource('dynamodb') 通过在 python 文件中使用上面的行,我可以从 ~/.aws 文件夹中获取凭据并在控制台中执行它。

But when I execute same code in Web-Browser, I am getting error (credentials are missing)但是当我在 Web 浏览器中执行相同的代码时,出现错误(缺少凭据)

I tried setting environment variables and tried using config and credentials files.我尝试设置环境变量并尝试使用配置和凭据文件。 But didn't work.但是没有用。

dynamodb = boto3.resource('dynamodb') dynamodb = boto3.resource('dynamodb')

ERRORRRR!!!!错误!!!!

raise NoCredentialsError

NoCredentialsError: Unable to locate credentials NoCredentialsError:无法找到凭据

The ~ in ~/.aws represents the home directory of the current user.~~/.aws代表当前用户的主目录。 When the script is executed from the console that works, because you are the current user.当脚本从有效的控制台执行时,因为您是当前用户。 When the script is executed via CGI the current user is some other account on your system, so ~ points to a different home directory where there is no credentials file.当脚本通过 CGI 执行时,当前用户是系统上的某个其他帐户,因此~指向一个不同的主目录,其中没有凭据文件。

If you are running this on an EC2 server, I would suggest switching to an IAM instance profile instead of a credentials file.如果您在 EC2 服务器上运行它,我建议切换到 IAM 实例配置文件而不是凭证文件。 Otherwise you will need to look into placing the credentials file under the correct user account home directory, or setting the credentials as environment variables to the CGI process.否则,您将需要考虑将凭据文件放置在正确的用户帐户主目录下,或将凭据设置为 CGI 进程的环境变量。

Based on Mark B answer I managed to solve a similar problem, I tried to for weeks..根据Mark B 的回答,我设法解决了类似的问题,我尝试了数周。

My python script is running flawlessly locally from IDEL on windows server我的 python 脚本在 Windows 服务器上从 IDEL 本地完美运行

However, when it called by web client ( java script) got the error但是,当它被 Web 客户端(java 脚本)调用时出现错误

botocore.exceptions.NoCredentialsError: Unable to locate credentials

The problem was exactly as Mark pointed related to home directory, when the script run locally, for example in windows, it is C:\\Users\\USER_NAME so it can find the credentials in the default location C:\\Users\\USER_NAME\\.aws\\credentials as the documentation states , however when it is run using CGI the home directory is different问题正是 Mark 指出的与主目录相关的问题,当脚本在本地运行时,例如在 Windows 中,它是 C:\\Users\\USER_NAME 所以它可以在默认位置 C:\\Users\\USER_NAME\\.aws 中找到凭据\\credentials 如文档所述,但是当它使用 CGI 运行时,主目录是不同的

So to fix that:所以要解决这个问题:

1 - Know what your home directory (During run-time) 1 - 知道你的主目录(在运行时)

from pathlib import Path

place these line before trying to access AWS service在尝试访问 AWS 服务之前放置这些行

home = str(Path.home())
print('Home %s'%home)

copy the full home path复制完整的主路径

2- Go to that directory and place a copy of .aws directory inside it. 2- 转到该目录并将 .aws 目录的副本放入其中。

3- Give the permission to read files inside .aws 3- 授予读取 .aws 文件的权限

For IIS server对于 IIS 服务器

cd {full_home_path} + \.aws  //Remove {} and + 
icacls . /grant "NT AUTHORITY\IUSR:(OI)(CI)(R)"
icacls . /grant "Builtin\IIS_IUSRS:(OI)(CI)(R)"

4- Reboot the server 4-重新启动服务器

5- Try python script from web client 5- 从 Web 客户端尝试 python 脚本

6- It should work. 6-它应该工作。

Typically, environments such as CGI run as the root user.通常,CGI 等环境以root用户身份运行。 However, this varies based upon how the service is configured.但是,这取决于服务的配置方式。

You have several options:您有多种选择:

  1. If the service is running as root , the home directory for root is /root .如果服务以root身份运行,则root的主目录为/root Create the directory /root/.aws .创建目录/root/.aws Then copy the contents of directory ~/.aws to /root/.aws .然后将目录~/.aws的内容复制到/root/.aws You will need to su to have the privileges.您需要su才能获得这些权限。
  2. Create a directory such as /AWS .创建一个目录,例如/AWS Copy your credentials to this directory.将您的凭据复制到此目录。 Now in your code specify the path to the credentials file /AWS/credentials .现在在您的代码中指定凭证文件/AWS/credentials的路径。
  3. Specify the aws_access_key and aws secret_access_key in your program.在您的程序中指定aws_access_keyaws secret_access_key This is not recommended as anytime you put your credentials in your program you open security risks.不建议这样做,因为任何时候您将凭据放入程序中都会带来安全风险。
  4. If your service is running in EC2, then assign a role to the instance and obtain your AWS access keys from metadata.如果您的服务在 EC2 中运行,则为该实例分配一个角色并从元数据中获取您的 AWS 访问密钥。

This AWS document explains in detail how credentials are managed.此 AWS 文档详细说明了凭证的管理方式。 Take the time to read it from top to bottom.花点时间从上到下阅读它。

Credentials 证书

I also recommend switching from using boto3 resource to boto3 client .我还建议从使用 boto3 resource切换到 boto3 client This will give you many more options in your code.这将为您的代码提供更多选项。

Example: Specify your credentials via hard coded credentials (not recommended):示例:通过硬编码凭据指定您的凭据(不推荐):

import boto3

session = boto3.Session(
    # Hard coded strings as credentials, not recommended.
    aws_access_key_id='AKIAIO5FODNN7EXAMPLE',
    aws_secret_access_key='ABCDEF+c2L7yXeGvUyrPgYsDnWRRC1AYEXAMPLE',
)

dynamodb = boto3.resource('dynamodb')

You can store your AWS credentials in any file format that you want and then read that file back to load the AWS access keys into your code.您可以以任何所需的文件格式存储您的 AWS 凭证,然后读回该文件以将 AWS 访问密钥加载到您的代码中。

I tried Amazon Translate with Google Colab and it's completely working fine.我用 Google Colab 尝试了 Amazon Translate,它完全运行良好。

Install boto3 client in cloud IDE在云IDE中安装boto3客户端

pip install boto3

Import client and it's configuration导入客户端及其配置

import boto3
from botocore.config import Config

Call Amazon Translate Service致电亚马逊翻译服务

translate = boto3.client(service_name='translate', region_name='us-east-2', use_ssl=True,aws_access_key_id = 'YOUR ACCESS KEY ID',aws_secret_access_key='YOUR ACCESS KEY' )
result = translate.translate_text(Text="YOUR TEXT TO TRANSLATE", SourceLanguageCode="en", TargetLanguageCode="ta")

You can access the result in JSON format您可以以 JSON 格式访问结果

print(result)

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

相关问题 Python CGI脚本,我无法将“ +”加运算符作为参数传递 - Python CGI script, i am not able to pass '+' plus operator as a parameter 使用cgi-python脚本设置co​​okie - set cookie with cgi-python scripts 我正在Python3中处理CGI脚本,它可以识别错误的目录 - I am working on a CGI script in Python3 and it recognizes wrong directory 在没有CGI模块的情况下使用Python作为CGI脚本吗? - Using Python as a CGI script without the CGI module? 为什么我不能加载早上生成的Excel文件,但可以使用Openpyxl在Python中下午加载它们 - Why I am not able to load excel files generated in the morning, but can load them in the afternoon in Python using Openpyxl 在 Python 脚本中传递 AWS 凭证 - Passing AWS Credentials in Python Script 如何使用cgi接受带有python脚本的发布请求? - How do I accept post requests with a python script using cgi? 当使用python脚本作为CGI时,Subprocess Popen无法运行后台进程 - When using the python script as CGI, Subprocess Popen not able to run background process 如何在python中使用mysql数据库调试cgi脚本。 我是如何调试的新手 - How do i debug my cgi script with mysql database in python. I am new to how to debugg 使用子进程的CGI脚本上的python errno 24 - python errno 24 on cgi script using subprocess
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM