2012-06-19 45 views
3

我正在使用Python中的boto庫連接到DynamoDB。下面的代碼已經爲我工作得很好:Amazon DynamoDB - 特定於區域的連接

import boto 
key = 'abc' 

secret = '123' 
con = boto.connect_dynamodb(key,secret) 
table = con.get_table('Table Name') 
-- rest of code -- 

當我嘗試連接到一個特定的區域,我可以連接就好了,但得到的表上工作時拋出一個錯誤:

import boto 
from boto.ec2.connection import EC2Connection 

key = 'abc' 
secret = '123' 
regions = EC2Connection(key,secret).get_all_regions() # some filtering after this line to remove unwanted entries 
for r in regions: 
    con = boto.connect_dynamodb(key,secret,region=r) 
    table = con.get_table('Table Name') # throws the error below 
    -- rest of code -- 

使用上面的第二個代碼塊,我得到一個ValueError: No JSON object could be decoded。調用con.list_tables()顯示了我在第一個代碼塊中查找的表格,但在第二個代碼塊中嘗試它時會引發相同的錯誤。誰能幫我嗎?

回答

4

玩弄後,我發現,更改代碼,以這種方式連接的工作原理:

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

key = 'abc' 
secret = '123' 
regions = EC2Connection(key,secret).get_all_regions() 
for r in regions: 
    con = connect_to_region(aws_access_key_id=key,aws_secret_access_key=secret,region_name=r.name) 
    table = con.get_table('Table Name') # no problem 
    -- rest of code --