python – Amazon DynamoDB – 特定于区域的连接
作者:互联网
我正在使用Python中的boto库连接到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:没有JSON对象可以被解码.调用con.list_tables()会在第一个代码块中显示我正在寻找的表,但是当我在第二个代码块中尝试它时会抛出相同的错误.谁能帮我吗?
解决方法:
玩完之后,我发现改变代码以这种方式连接起作用:
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 --
标签:python,amazon-web-services,amazon-dynamodb,boto 来源: https://codeday.me/bug/20190902/1788016.html