我在端口8080上运行DynamoDB Local。这是用来启动DB的命令:
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -port 8080
转到localhost:8080/shell/
将生成DynamoDB Javascript Shell。shell工作并允许在本地db文件上执行操作。使用shell创建的db文件名为cUniqueSessionID_us-west-2.db
。
在PHP项目中,我使用以下代码使用DynamoDB
创建一个表$db = new 'Aws'DynamoDb'DynamoDbClient([
'region' => 'us-west-2',
'version' => 'latest',
'endpoint' => 'http://localhost:8080/',
'credentials' => [
'key' => 'not-a-real-key',
'secret' => 'not-a-real-secret',
],
]);
$db->createTable([
'TableName' =>'SampleTable',
'AttributeDefinitions' => [
[ 'AttributeName' => 'Id', 'AttributeType' => 'N' ]
],
'KeySchema' => [
[ 'AttributeName' => 'Id', 'KeyType' => 'HASH' ]
],
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 5,
'WriteCapacityUnits' => 6
]
]);
但是运行这个得到Error executing "CreateTable" on "http://localhost:8080/"; AWS HTTP error: cURL error 7: Failed to connect to localhost port 8080: Connection refused
我试过关闭防火墙,但没有成功。是什么导致了这个错误?
我发现问题了。
我的PHP项目在Vagrant虚拟机中运行,但我在本地机器上运行DynamoDB。因此,我的PHP代码中的localhost
引用了流浪的localhost,但是DynamoDB运行在我的机器的localhost上。
为了解决这个问题,我在Vagrant中运行了DynamoDB。