简体   繁体   English

来自 php 的 AWS dynamodb 查询

[英]AWS dynamodb query from php

I have a react application and i'm trying to use aws dynamodb, i installed the php sdk but i don't know how to query my db.我有一个反应应用程序,我正在尝试使用 aws dynamodb,我安装了 php sdk 但我不知道如何查询我的数据库。

I copied the tutorial here and i changed the endpoint to: "https://dynamodb.us-west-2.amazonaws.com".我在这里复制了教程,并将端点更改为:“https://dynamodb.us-west-2.amazonaws.com”。

I get this error: {"__type":"com.amazon.coral.service#UnrecognizedClientException","message":"The security token included in the request is invalid."}.我收到此错误:{"__type":"com.amazon.coral.service#UnrecognizedClientException","message":"请求中包含的安全令牌无效。"}。 I guess i have to add a security token somewhere, i don't know where and neither where to find it.我想我必须在某处添加一个安全令牌,我不知道在哪里也不知道在哪里可以找到它。

Any suggestion?有什么建议吗?

Based on your error, i think you need to check your aws secret key and access key.根据您的错误,我认为您需要检查您的 aws 密钥和访问密钥。 You can try to install aws cli then create user access programmatically from aws console from this link您可以尝试安装 aws cli,然后通过此链接从 aws 控制台以编程方式创建用户访问

Then you can try your source code after that.然后您可以在此之后尝试您的源代码。

The following code example shows how to get an item from a DynamoDB table.以下代码示例展示了如何从 DynamoDB 表中获取项目。

// '/path/to/aws-autoloader.php' to import AWS SDKs.
require 'vendor/autoload.php';

use Aws\DynamoDb\DynamoDbClient;
use Aws\Exception\AwsException;
use Aws\DynamoDb\Exception\DynamoDbException;

// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest'
]);

// Use an Aws\Sdk class to create the dynamoDbClient object.
$dynamoDbClient = $sdk->createDynamoDb();

try {
    $dynamoDbClient->getItem([
                'Key'       => [
                    'id' => [
                        'N' => 1,
                    ],
                ],
                'TableName' => 'products',
            ]);
} catch (DynamoDbException $e) {
    // Catch a DynamoDb-specific exception.
    echo $e->getMessage();
} catch (AwsException $e) {
    // This catches the more generic AwsException. You can grab information
    // from the exception using methods of the exception object.
    echo $e->getAwsRequestId() . "\n";
    echo $e->getAwsErrorType() . "\n";
    echo $e->getAwsErrorCode() . "\n";

    // This dumps any modeled response data, if supported by the service
    // Specific members can be accessed directly (e.g. $e['MemberName'])
    var_dump($e->toArray());
}

Notice that we did not explicitly provide credentials to the client.请注意,我们没有明确向客户端提供凭据。 That's because the SDK should detect the credentials from environment variables (via AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY ), an AWS credentials INI file in your HOME directory, AWS Identity and Access Management (IAM) instance profile credentials, or credential providers.这是因为 SDK 应该检测来自环境变量的凭证(通过AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY )、 HOME目录中的 AWS 凭证 INI 文件、AWS Identity and Access Management (IAM) 实例配置文件凭证或凭证提供程序。 If we don't provide a credentials option, the SDK attempts to load credentials from your environment in the following order:如果我们不提供凭据选项,SDK 会尝试按以下顺序从您的环境中加载凭据:

  1. Load credentials from environment variables .环境变量加载凭据。
  2. Load credentials from a credentials.ini file .credentials.ini 文件加载凭据。
  3. Load credentials from IAM role .IAM 角色加载凭证。

We can also directly create the service-specific client object like below:我们也可以像下面这样直接创建特定于服务的客户端 object:

$dynamoDbClient = new DynamoDbClient(
        [
            'region'  => 'us-west-2',
            'version' => 'latest',
        ]

But AWS highly recommended that you use the Sdk class to create clients if you're using multiple client instances in your application.但如果您在应用程序中使用多个客户端实例,AWS 强烈建议您使用 Sdk class 创建客户端。 As per AWS docs:-根据 AWS 文档:-

The Sdk class automatically uses the same HTTP client for each SDK client, allowing SDK clients for different services to perform nonblocking HTTP requests. Sdk class自动为每个SDK客户端使用相同的HTTP客户端,允许不同服务的SDK客户端执行非阻塞的HTTP请求。 If the SDK clients don't use the same HTTP client, then HTTP requests sent by the SDK client might block promise orchestration between services.如果SDK客户端不使用相同的 HTTP 客户端,则 SDK 客户端发送的 HTTP 请求可能会阻止服务之间的 promise 编排。

You can refer to the AWS document pages:-您可以参考 AWS 文档页面:-

  1. AWS SDK PHP - BASIC USAGE AWS SDK PHP - 基本用法
  2. AWS SDK PHP - DynamoDB Examples AWS SDK PHP - DynamoDB 示例
  3. AWS SDK PHP - Configuration guide AWS SDK PHP - 配置指南
  4. AWS SDK PHP - APIs AWS SDK PHP - API

I hope this helps.我希望这有帮助。

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

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