如何访问Python SDK boto3的安全令牌

2024-10-02 14:26:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从python脚本访问AWS理解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.


Tags: thekeytextnamein脚本clientaws
2条回答

我每天使用的一个有用的工具是:https://github.com/atward/aws-profile/blob/master/aws-profile

这使得承担这个角色更加容易!在

在.aws/credentials和.aws/config中设置访问密钥后

你可以这样做:

AWS_PROFILE=**you-profile** aws-profile [python x.py]

[]中的部分可以替换为任何您想要使用AWS凭证的内容。e、 g.地形图

实际上,这个实用程序只需将您的AWS凭证放入os环境变量中。然后在boto脚本中,您不需要担心设置aws_access_key_id等。。在

此错误表明您提供了无效的凭据。在

你永远不要在源代码中放入凭证,这也是毫无价值的。如果其他人获得对源代码的访问权,这可能会导致潜在的安全问题。在

有几种方法可以向使用awsdk的应用程序提供有效的凭证(例如boto3)。在

如果应用程序在amazonec2实例上运行,请为该实例分配一个IAM角色。这将自动提供可由boto3检索的凭据。在

如果您正在自己的计算机上运行应用程序,请将凭据存储在.aws/credentials文件中。创建此文件的最简单方法是使用aws configure命令。在

参见:Credentials — Boto 3 documentation

相关问题 更多 >