Python boto不改变区域

时间:2015-10-20 01:25:52

标签: python boto

不确定为什么但是每次我尝试添加一个区域时我都会得到22个发电机表(错误),但是如果我不总是得到正确的230(us-east-1),为什么区域不是在boto中设置正确以列出表格?

我相当肯定它与这一段代码具体相关

    if not region == '*':
        # Always lists 22 tables ???
        conn = boto.dynamodb2.connect_to_region(region, aws_access_key_id=key, aws_secret_access_key=secret)
        conn = boto.connect_dynamodb(conn)
    else:
        # Lists 230 tables (what I have in us-east-1)
        conn = boto.connect_dynamodb(key, secret)

不幸的是,我不知道是什么。这是代码的其余部分

## Dynamo Tables
def tables(self, region = False, called = False):
    if region == False:
        region = self.regions(True)

    key, secret = self.getCredentials()

    if not region == '*':
        # Always lists 22 tables ???
        conn = boto.dynamodb2.connect_to_region(region, aws_access_key_id=key, aws_secret_access_key=secret)
        conn = boto.connect_dynamodb(conn)
    else:
        # Lists 230 tables (what I have in us-east-1)
        conn = boto.connect_dynamodb(key, secret)

    tbls = conn.list_tables()
    print 'There are %s tables\n' % len(tbls)

    #for tbl in tbls:
    #    print tbl

    if called:
        tableInput = int(raw_input("\nEnter table number: "))
        if int(tableInput) > len(tbl):
            print 'Table %s is not available' % tableInput
            exit(0)

        return tbl[tableInput - 1]
    else:
        exit(1)

def getCredentials(self):
    config = ConfigParser.ConfigParser()
    filename = os.path.join(os.path.expanduser('~'), '.aws/credentials')

    if os.path.isfile(filename):
        config.read(filename)
        i = 1

        for section in config.sections():
            print '%03d' % i, ':', section
            i += 1

        section = config.sections()[int(raw_input("\nEnter config section: ")) - 1]
        print config.get(section, 'aws_secret_access_key')
        # exit(0)

        # return key, secret
        return config.get(section, 'aws_access_key_id'), \
               config.get(section, 'aws_secret_access_key') 

1 个答案:

答案 0 :(得分:0)

#!/usr/bin/env python

from boto.ec2.connection import EC2Connection
from boto.dynamodb import connect_to_region

key     = ''
secret  = ''
regions = EC2Connection(key, secret).get_all_regions()

for r in regions:
  conn = connect_to_region(aws_access_key_id=key,aws_secret_access_key=secret,region_name=r.name)
  print 'Using region: ', r.name
  print 'Total found:  ', len(conn.list_tables())