Amazon DynamoDB — region-specific connection

隐身守侯 提交于 2019-12-10 18:14:46

问题


I'm using the boto library in Python to connect to DynamoDB. The following code has been working for me just fine:

import boto
key = 'abc'

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

When I try to connect to a specific region, I can connect just fine, but getting the table to work on is throwing an error:

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 --

Using the second block of code above, I get a ValueError: No JSON object could be decoded. Calling con.list_tables() shows the table I'm looking for in the first code block, but throws the same error when I try it in the second code block. Can anyone help me out?


回答1:


After playing around, I found out that changing the code to connect in this fashion works:

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 --


来源:https://stackoverflow.com/questions/11101195/amazon-dynamodb-region-specific-connection

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