【问题标题】:How to display dynamodb table names of respective user如何显示各个用户的 dynamodb 表名
【发布时间】:2017-03-10 10:40:42
【问题描述】:

dynamodb 的 php 代码。我在使用 ListTables() api 调用显示表名时遇到问题。我就是这样做的。

STEP1:在 AWS IAM 中,我在 IAM 中创建了两个名为“user1”和“user2”的用户。 IAM 控制台为用户“user1”和“user2”创建了凭证(访问密钥和秘密密钥)。在 dynamodb 中,我创建了两个表 Books 和 Users。

步骤 2:对于 user1,我只允许访问名为“Books”的表。我写的政策文件如下(在资源下,我提到了表名'Books'的ARN。)

{
    "Version": "2012-10-17",
    "Statement": [
    {
        "Action": [                 
             "dynamodb:BatchGetItem",
             "dynamodb:DescribeTable",
             "dynamodb:GetItem",
             "dynamodb:ListTables",
             "dynamodb:Query",
             "dynamodb:Scan",
             "dynamodb:DescribeReservedCapacity",
             "dynamodb:DescribeReservedCapacityOfferings",
             "dynamodb:ListTagsOfResource"                 
         ],
         "Effect": "Allow",
         "Resource": "arn:aws:dynamodb:us-east-1:468737190093:table/Books",

     }]
}

步骤 3:对于 user2,我只允许访问名为“Users”的表。我写的政策文件如下(在资源下,我提到了表名'Users'。)

{
    "Version": "2012-10-17",
    "Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "dynamodb:BatchGetItem",
            "dynamodb:DescribeTable",
            "dynamodb:GetItem",
            "dynamodb:ListTables",
            "dynamodb:Query",
            "dynamodb:Scan",
            "dynamodb:DescribeReservedCapacity",
            "dynamodb:DescribeReservedCapacityOfferings",
            "dynamodb:ListTagsOfResource"
        ],
        "Resource": [
            "arn:aws:dynamodb:us-east-1:468737190093:table/Users"
        ]

    }]
}

现在我的目标是显示各个用户的表名。如果我使用 user1 登录(使用 user1 的凭据访问密钥和密钥),我只需要显示表名“书籍”。或者如果我使用 user2 登录(使用 user2 的凭据:accesskey 和密钥),我只需要显示“用户”。 我尝试使用 ListTables() api 调用。但没有达到我想要的。

$sdk = new Aws\Sdk([
    'region' => 'us-east-1',
    'version' => 'latest',
    'credentials' => array(
        'key' => $userAccessKey,
        'secret' => $userSecretKey
    ),
    'http' => [
        'verify' => 'D:\xampp\htdocs\ssl\cacert.pem'
    ]
]);
$dynamodb = $sdk->createDynamoDb();
$result = $dynamodb->listTables();



 echo '<ul>';
    foreach ($result['TableNames'] as $tableName) {   
        echo '<li><a href="table.php?var1='.$tableName.'">'.$tableName.'</a>              
    </li>';
 }
echo '</ul>';

我遇到了以下异常: AccessDeniedException","Message":"用户:arn:aws:iam::468737190093:user/user1 无权执行:dynamodb:ListTables on resource: *

注意:如果我在策略文档中将“资源”字段更改为“*”,则它会成功显示所有表名(书籍、用户)。但我只需要显示各个用户的表名(user1 --> 只显示'Books',user2--> 只显示'Users')。

如果不是 ListTables(),dynamodb 中是否有任何其他 api 调用或任何其他方法可以列出表名? 有人可以给我解决方案.. :) 提前致谢。

【问题讨论】:

    标签: php amazon-web-services


    【解决方案1】:

    我有一个解决方案。我使用错误代码来解决这个问题。 (我放了 try catch 块来捕捉错误)。

    第一步: 更改策略文档,使用户拥有两种权限。 1. 使用以下资源访问一些 dynamodb api 调用的权限,如 BatchGetItem()、DescribeTable()...等(资源名称为表 'Books' 的 arn)
    “资源”:“arn:aws:dynamodb:us-east-1:468737190093:table/Books”,

    1. 使用以下资源(所有表的资源名称)访问 api 调用 ListTables 的权限。 “资源”:“*”

      {

      “版本”:“2012-10-17”,

      "Statement": [
       {
          "Action": [                 
               "dynamodb:BatchGetItem",
               "dynamodb:DescribeTable",
               "dynamodb:GetItem",
               "dynamodb:ListTables",
               "dynamodb:Query",
               "dynamodb:Scan",
               "dynamodb:DescribeReservedCapacity",
               "dynamodb:DescribeReservedCapacityOfferings",
               "dynamodb:ListTagsOfResource"                 
           ],
           "Effect": "Allow",
           "Rsource": "arn:aws:dynamodb:us-east-1:468737190093:table/Books"
       },
       {
              "Action": [
                  "dynamodb:ListTables"
              ],
              "Effect": "Allow",
              "Resource": "*"
        }
      

      ] }

    也将权限更改为 user2(与 user1 相同)。

    第二步: 使用 listtables() api 获取所有表格。 $result = $dynamodb->listTables();

    step3:定义一个数组userTables。对每个表执行 scan()。如果我们遇到带有错误代码的异常:“AccessDeniedException”,则不要将该表名添加到数组中。将其余表名添加到数组中。

    $userTables = array();
    foreach ($result['TableNames'] as $tableName) {         
    
    
            try
            {
                    $result1 = $dynamodb->scan([
                        'TableName' => $tableName
                    ]);
            }
            catch(\Aws\DynamoDb\Exception\DynamoDbException $e)
            {               
                    $errCode = $e->getAwsErrorCode();
                    //echo "<br>***Error Code: $errCode";
                    if($errCode == "AccessDeniedException")             
                    {
                            //echo "<br>No Permission to access the table named: <b>$tableName</b><br>";
                            continue; // Don't add this table, because the user don't have permission to this table.
    
                    }
            }
    
            // Add this table name to array userTables.
            array_push($userTables, $tableName);
    
    }
    

    step4:使用数组 userTables 显示表名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      相关资源
      最近更新 更多