在使用Amazon DynamoDB时,如果遇到“AmazonDynamoDB异常:用户:用户名未被授权执行对资源的dynamodb:DescribeTable操作。”的错误,这表示当前用户没有足够的权限来执行DescribeTable操作。
要解决此问题,您需要为当前用户授予执行DescribeTable操作所需的权限。以下是使用AWS SDK for Python(Boto3)的代码示例来解决此问题:
import boto3
# 创建DynamoDB客户端
dynamodb_client = boto3.client('dynamodb')
# 设置要授予权限的用户和表名
user_arn = 'arn:aws:iam::123456789012:user/your-username'
table_name = 'your-table-name'
# 为用户授予DescribeTable权限
response = dynamodb_client.update_table(
TableName=table_name,
AttributeDefinitions=[
{
'AttributeName': 'id',
'AttributeType': 'N'
}
],
KeySchema=[
{
'AttributeName': 'id',
'KeyType': 'HASH'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
},
StreamSpecification={
'StreamEnabled': False
},
SSESpecification={
'Enabled': False
},
BillingMode='PROVISIONED',
Tags=[
{
'Key': 'Name',
'Value': 'YourTable'
},
],
SSEEnabled=False
)
# 打印响应
print(response)
在上面的示例中,我们使用boto3.client('dynamodb')创建了DynamoDB客户端。然后,我们使用update_table方法为特定的用户和表名授予DescribeTable操作的权限。
您需要将your-username替换为实际的用户名,并将your-table-name替换为实际的表名。还可以根据需要修改其他参数。
请确保使用具有足够权限的凭证来执行此操作。