changes using boto3 for connections to aws services

╄→尐↘猪︶ㄣ 提交于 2020-01-11 14:10:11

问题


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 AWS
  • boto3 resource: A higher-level set of objects that make it easier to interact with resources, rather than using standard API calls (eg vpc.subnets() vs describe-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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!