使用AWS API Gateway SDK可以实现与Amplify API类似的功能。如下是一个示例:
import AWS from 'aws-sdk';
import { APIGatewayClient, GetItemCommand } from "@aws-sdk/client-api-gateway";
const region = 'your-region';
const accountId = 'your-account-id';
const restApiId = 'your-rest-api-id';
const stage = 'your-stage';
const url = `https://${restApiId}.execute-api.${region}.amazonaws.com/${stage}`;
AWS.config.update({region});
const apiGatewayClient = new APIGatewayClient({region});
const getItemCommand = new GetItemCommand({
TableName: 'your-dynamodb-table-name',
Key: {
'id': {S: 'your-item-id'}
}
});
try {
const data = await apiGatewayClient.send(getItemCommand);
console.log('Data:', data);
} catch (error) {
console.error('Error:', error);
}
在这个示例中,我们使用AWS SDK和API Gateway Client实例化一个API网关客户端。然后,我们执行一个HTTP GET请求(使用GetItemCommand
)来获取DynamoDB表中特定项的数据。您可以使用同样的方式执行PUT、POST或DELETE请求。