python-3.x - 如何访问现有的 Amazon DynamoDB 表?

标签 python-3.x amazon-web-services amazon-dynamodb

一般问题

我关注了this tutorial从 Android 应用程序创建和访问 Amazon DynamoDB,然后将其改编为在我正在编写的应用程序中使用。尽管我在启动和运行它时遇到了困难,但它工作得很好。但是,我还希望能够使用在我的 Raspberry Pi 上运行的 python 脚本访问数据库。

我找到了 this tutorial , 但它似乎只描述了如何与本地 DynamoDB 表进行交互。

具体问题

以下代码连接一个项目并将其写入 DynamoDB 表。我找不到我的 Amazon DynamoDB 的任何类型的端点 URL,只有 ARN,并且没有像我在我的应用程序中使用的那样传递密码或用户名。

# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if o % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)    

dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")    

table = dynamodb.Table('Movies')    

title = "The Big New Movie"
year = 2015    

response = table.put_item(
   Item={
        'year': year,
        'title': title,
        'info': {
            'plot':"Nothing happens at all.",
            'rating': decimal.Decimal(0)
        }
    }
)

我已经搜索了有关连接到 Amazon DynamoDB 实例的任何类型的说明,但我找到的所有内容都描述了一个本地表。如果有人可以针对特定问题提供建议或推荐教程,我将不胜感激。

最佳答案

改变

dynamodb = boto3.resource('dynamodb',endpoint_url="http://localhost:8000") 

dynamodb = boto3.resource('dynamodb',region_name='REGION') 

其中 REGION 是您的 dynamodb 区域的名称,例如“us-west-2”。然后它将连接到 AWS DynamoDB 实例。

编辑:如果您还没有这样做,您将需要设置您的 AWS Credentials .有几种选择。一种选择是使用环境变量

Boto3 will check these environment variables for credentials:

AWS_ACCESS_KEY_ID The access key for your AWS account.

AWS_SECRET_ACCESS_KEY The secret key for your AWS account.

关于python-3.x - 如何访问现有的 Amazon DynamoDB 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47643411/

相关文章:

python - 应用条件格式(没有错误),但工作表不会更新,直到我手动转到 'Edit Rules'

amazon-web-services - 如何一次更新 DynamoDB 表中的多个项目

amazon-web-services - 如何正确地将 Docker Compose 部署到 Amason EC2 实例?

ios - 如何将认知用户池与身份池集成?

oracle - 有没有办法配置 Elastic Beanstalk 以连接到现有的 Oracle 数据库(没有 BYOL)?

nosql - DynamoDB 相对于 Google Cloud Datastore 的优缺点是什么

kotlin - 如何在不删除整行的情况下仅从 dynamo Db 行中删除一个属性?

python-3.x - 为什么我的 cron 命令没有在日志文件中输出?

python - 使用 python-3.x 从 zip 存档中读取 CSV 文件

python - 获取类型错误 : __init__() missing 1 required positional argument: 'on_delete' when trying to add parent table after child table with entries