如何访问 Python SDK boto3 的安全令牌
How to I access Security token for Python SDK boto3
我想从 python 脚本访问 AWS comprehend api。没有得到任何关于如何删除此错误的线索。我知道一件事,我必须获得会话安全令牌。
try:
client = boto3.client(service_name='comprehend', region_name='us-east-1', aws_access_key_id='KEY ID', aws_secret_access_key= 'ACCESS KEY')
text = "It is raining today in Seattle"
print('Calling DetectEntities')
print(json.dumps(client.detect_entities(Text=text, LanguageCode='en'), sort_keys=True, indent=4))
print('End of DetectEntities\n')
except ClientError as e:
print (e)
Error : An error occurred (UnrecognizedClientException) when calling the DetectEntities operation: The security token included in the request is invalid.
此错误表明您提供的凭据无效。
您应该永远不要将凭据放入您的源代码中,这也是毫无意义的。如果其他人可以访问源代码,这可能会导致潜在的安全问题。
有多种方法可以向使用 AWS SDK 的应用程序(例如 boto3
)提供有效凭证。
如果应用程序在 Amazon EC2 实例上 运行,请为该实例分配一个 IAM 角色。这将自动提供可由 boto3 检索的凭据。
如果您是 运行 您自己计算机上的应用程序,请将凭据存储在 .aws/credentials
文件中。创建此文件的最简单方法是使用 aws configure
命令。
我每天使用的一个有用工具是:https://github.com/atward/aws-profile/blob/master/aws-profile
这让担任角色变得容易多了!
在 .aws/credentials 和 .aws/config
中设置访问密钥后
你可以这样做:
AWS_PROFILE=**you-profile** aws-profile [python x.py]
[]
中的部分可以替换为您想要使用 AWS 凭证的任何内容。例如,地形规划
本质上,此实用程序只是将您的 AWS 凭证放入 os 环境变量中。然后在你的 boto 脚本中,你不需要担心设置 aws_access_key_id 等..
使用 aws configure
或更新 ~/.aws/config
创建配置文件。如果您只有一个配置文件可以使用 = default
,您可以从 Session()
调用中省略 profile_name
参数(参见下面的示例)。然后使用会话对象创建特定于 AWS 服务的客户端。例子;
import boto3
session = boto3.session.Session(profile_name="test")
ec2_client = session.client('ec2')
ec2_client.describe_instances()
ec2_resource = session.resource(‘ec2’)
我想从 python 脚本访问 AWS comprehend api。没有得到任何关于如何删除此错误的线索。我知道一件事,我必须获得会话安全令牌。
try:
client = boto3.client(service_name='comprehend', region_name='us-east-1', aws_access_key_id='KEY ID', aws_secret_access_key= 'ACCESS KEY')
text = "It is raining today in Seattle"
print('Calling DetectEntities')
print(json.dumps(client.detect_entities(Text=text, LanguageCode='en'), sort_keys=True, indent=4))
print('End of DetectEntities\n')
except ClientError as e:
print (e)
Error : An error occurred (UnrecognizedClientException) when calling the DetectEntities operation: The security token included in the request is invalid.
此错误表明您提供的凭据无效。
您应该永远不要将凭据放入您的源代码中,这也是毫无意义的。如果其他人可以访问源代码,这可能会导致潜在的安全问题。
有多种方法可以向使用 AWS SDK 的应用程序(例如 boto3
)提供有效凭证。
如果应用程序在 Amazon EC2 实例上 运行,请为该实例分配一个 IAM 角色。这将自动提供可由 boto3 检索的凭据。
如果您是 运行 您自己计算机上的应用程序,请将凭据存储在 .aws/credentials
文件中。创建此文件的最简单方法是使用 aws configure
命令。
我每天使用的一个有用工具是:https://github.com/atward/aws-profile/blob/master/aws-profile
这让担任角色变得容易多了!
在 .aws/credentials 和 .aws/config
中设置访问密钥后你可以这样做:
AWS_PROFILE=**you-profile** aws-profile [python x.py]
[]
中的部分可以替换为您想要使用 AWS 凭证的任何内容。例如,地形规划
本质上,此实用程序只是将您的 AWS 凭证放入 os 环境变量中。然后在你的 boto 脚本中,你不需要担心设置 aws_access_key_id 等..
使用 aws configure
或更新 ~/.aws/config
创建配置文件。如果您只有一个配置文件可以使用 = default
,您可以从 Session()
调用中省略 profile_name
参数(参见下面的示例)。然后使用会话对象创建特定于 AWS 服务的客户端。例子;
import boto3
session = boto3.session.Session(profile_name="test")
ec2_client = session.client('ec2')
ec2_client.describe_instances()
ec2_resource = session.resource(‘ec2’)