简体   繁体   English

在不使用 boto3 的情况下改变 DynamoDB 中的表

[英]Mutate table in DynamoDB without using boto3

I am trying to create a Python project that is able to both query and update tables in my AWS Dynamo database.我正在尝试创建一个 Python 项目,该项目能够在我的 AWS Dynamo 数据库中查询和更新表。

My query is as follows:我的查询如下:

query = """query {
    listTodos {
    nextToken
    startedAt
    items {
      createdAt
      name
      description
    }
  }
}"""


Headers = {'X-API-KEY': 'myKey'}
url = 'https://example.amazonaws.com/graphql'
r = requests.post(url, json={'query': query}, headers=Headers)
print(r.status_code)
print(r.text)

I get an expected 200 response that gives me the name, createdAt (time), and description.我收到了预期的 200 响应,其中给出了名称、createdAt(时间)和描述。

What I am trying to do is pass in a mutation, but there is no documentation for this when not using boto3.我想要做的是传递一个突变,但是当不使用 boto3 时没有这方面的文档。 For simplicity, I am curious to know how I would mutate this table while just using requests.post为简单起见,我很想知道在仅使用 requests.post 时如何更改此表

I have tried the following with no success:我尝试了以下但没有成功:

query = """
    mutation {
        updateTodo (id: 80e1a712-0053-436c-ba73-9182a7cca67f, name: "Paul") {
            name
        }
    }
"""

From this I recieve MalformedHttpRequestException从这里我收到 MalformedHttpRequestException

What is the best way to accomplish this?实现这一目标的最佳方法是什么? Thank you in advance, as I am new to the AWS platform.提前感谢您,因为我是 AWS 平台的新手。

In a blog post I once compared the DynamoDB request protocol to another protocol, CQL.在一篇文中,我曾经将 DynamoDB 请求协议与另一个协议 CQL 进行了比较。 One of my observations was that one of the most "obvious" advantages of the DynamoDB protocol is that it uses the standard HTTP and JSON for requests, so at first glance it appears as if it is trivial to write a DynamoDB client in any language that has an HTTP request library - without needing a complex AWS-specific library like boto3.我的观察之一是 DynamoDB 协议最“明显”的优势之一是它使用标准的 HTTP 和 JSON 来处理请求,所以乍一看,用任何语言编写 DynamoDB 客户端似乎都是微不足道的有一个 HTTP 请求库 - 不需要像 boto3 这样复杂的 AWS 特定库。

So, why do people end up using boto3 (for Python) anyway?那么,为什么人们最终还是使用 boto3(用于 Python)呢? Well, the main reason is the need to sign requests.嗯,主要原因是需要签署请求。 You didn't say why your attempts were not successful (what error did you get?) but if I ignore for a moment the fact that your request doesn't look like a valid DynamoDB request, the next problem you'd encounter is Amazon complaining that your request is not signed.您没有说明为什么您的尝试不成功(您遇到了什么错误?)但是如果我暂时忽略您的请求看起来不像有效的 DynamoDB 请求这一事实,那么您遇到的下一个问题是 Amazon抱怨您的请求未签名。 You need to digitally sign each and every request using your AWS id and secret key, to prove who you are and allow Amazon to charge you for the request.您需要使用您的 AWS id 和密钥对每个请求进行数字签名,以证明您的身份并允许 Amazon 向您收取请求费用。 Doing this without a library is very possible (I have done it), but not trivial.在没有库的情况下这样做是非常有可能的(我已经做到了),但并非微不足道。 It's much easier to just use a library, like boto3.只使用一个库要容易得多,比如 boto3。 Why are you trying to avoid it?你为什么要避免它?

If you are really serious about avoiding boto3 (or other alternative libraries) and implementing the protocol yourself, please take a look at Amazon's documentation on the low-level API for more details on DynamoDB's request format and the signature implementation.如果您真的很想避免使用 boto3(或其他替代库)并自己实现该协议,请查看亚马逊关于低级 API 的文档,了解有关 DynamoDB 请求格式和签名实现的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM