尝试导入lambd时没有名为“requests\u aws4auth”的模块

2024-10-05 14:26:00 发布

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

我需要我的lambda调用API网关,并在我的云形成模板中将以下代码作为lambda的内联代码。

from requests_aws4auth import AWS4Auth
def handler(event,context):
          client = boto3.client('sts')
          responseAssumeRole = client.assume_role(
            DurationSeconds=3600,
            RoleArn='arn',// real arn of the api gateway invocation role
            RoleSessionName='Bob',
          )
          credentials = responseAssumeRole['Credentials']
          auth = AWS4Auth(aws_access_key=responseAssumeRole['Credentials']['AccessKeyId'],
                                 aws_secret_access_key=responseAssumeRole['Credentials']['SecretAccessKey'],
                                 aws_host='host.execute-api.us-east-1.amazonaws.com',
                                 aws_region='us-east-1',
                                 aws_service='execute-api')
          headers= {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}
          response = requests.get('https://host.execute-api.us-east-1.amazonaws.com/test',
                                  auth=auth, headers=headers)

这给了我以下错误

No module named 'requests_aws4auth'

也欢迎使用aws凭据创建身份验证的任何解决方案或替代方法。


Tags: lambda代码clientauthawsapihostexecute
2条回答

也许您也可以使用上下文初始值设定项来解决此问题,如下面的答案所示:

Spring Environment backed by Typesafe Config

TL;博士

如果无法在自定义impl中处理path,则返回null

public class TypesafeConfigPropertySource extends PropertySource<Config> {
    // ...    
    @Override
    public Object getProperty(String path) {
        try {
            if (source.hasPath(path)) {
                return source.getAnyRef(path);
            }
        } catch(ConfigException.BadPath ignore) {
        }
        return null;
    }
    // ...
}

解释

I am making educated guesses, but functionally this appears supported by the way the code behaves

最有可能的场景是,在任何默认实现之前,解析顺序将考虑我们的自定义实现。在我们的实现中使用的方法将与包含“:”和“[”的任何路径一起出错,因为在检查路径是否存在时出错

我只是包装BadPath异常以捕获任何问题,然后返回null表示不匹配

相关问题 更多 >