boto3从客户端获取资源 [英] boto3 Get a resource from a client

查看:50
本文介绍了boto3从客户端获取资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

适用于python的AWS库(boto)具有两种与AWS配合使用的接口类型,即低级 client 和高级别的pythonic resource .

The AWS Library for python (boto) has two different types of interfaces for working with AWS, a low level client and a higher level more pythonic resource.

我的代码的一部分使用一个,而其他部分使用另一个.

Parts of my code use one, while other parts use the other.

资源获取 client .

# Create the resource
sqs_resource = boto3.resource('sqs')

# Get the client from the resource
sqs = sqs_resource.meta.client

我的问题是,如果有客户 sqs ,如何从中获取 boto3.resource ?

My questions is if have the client sqs, how do I get a boto3.resource from this?

(我不能只是简单地调用 boto3.resource('sqs'),因为客户端还有其他东西,例如已经附加了凭据,出于某种设计原因,资源会尝试获取来自我不想要的地方的AWS凭证,我希望它使用客户端上设置的任何凭证/帐户)

(I can't simply just call boto3.resource('sqs') because the client has other things, such as credentials already attached to it, the resource for some design reason tries to fetch the AWS credentials from a bunch of places I don't want it to, I'd like it to use whatever credentials/account is set on the client)

推荐答案

无法执行此操作.如果要同时使用两者,则应创建资源并使用嵌入式客户端.您可以使用与客户端完全相同的配置实例化资源.资源的基础客户端以完全相同的方式创建.资源的客户端和使用完全相同的参数创建的客户端之间的唯一区别是,资源客户端将资源"添加到用户代理.

There is no way to do this. If you want to use both, you should create a resource and use the embedded client. You can instantiate a resource with the exact same configuration as a client. The underlying client for the resource is created in the exact same way. The only difference between a resource's client and a client created with the exact same parameters is that the resource client adds 'Resource' to the user-agent.

这篇关于boto3从客户端获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆