问题
What all changes has to be done while using a function which was using boto2 earlier and how has to be changes to boto3 below is one such function example which is on boto2 and it needs to be changed to boto3
def aws(serviceName, module=boto):
conn = connections.get(serviceName)
if conn is None:
service = getattr(module, serviceName)
conn = service.connect_to_region(region)
connections[serviceName] = conn
return conn
回答1:
That code doesn't seem to be doing much. It is simply connecting to an AWS service.
The boto3 equivalent is probably:
client = boto3.client(serviceName)
The region can be defined in the standard .aws/config
file, or as:
client = boto3.client(serviceName, region_name='ap-southeast-2')
I recently converted some code from boto
to boto3
and every line pretty much needed changing. The result, however, was a lot cleaner.
It is also worth trying to understand the difference between:
boto3
client: Makes normal API calls to AWSboto3
resource: A higher-level set of objects that make it easier to interact with resources, rather than using standard API calls (egvpc.subnets()
vsdescribe-subnets(VPC=xxx)
)
The original code block appears to be storing its information in a connections
array (defined elsewhere) for re-use. Therefore, the equivalent code block would be:
def aws(serviceName):
conn = connections.get(serviceName)
if conn is None:
conn = boto3.client(serviceName, region)
connections[serviceName] = conn
return conn
来源:https://stackoverflow.com/questions/52051700/changes-using-boto3-for-connections-to-aws-services