5

我知道我在这里遗漏了一些基本的东西,但我真的被困在试图访问 AWS IOT 平台上的东西影子。

我正在使用以下代码来创建一个新事物:

use Aws\Iot\IotClient;
$thingName = '<string uuid>';
$awsIoTClient = new IotClient([
    'version' => 'latest',
    'region' => <region>,
    'credentials' => [
      'key'    => <aws_access_key>,
      'secret' => <aws_secret_key>,
    ]
]);
$policyName = 'Global_Hub_Policy';
// # !---------------------------
// # !- Implementation
// # !---------------------------
$result = $awsIoTClient->createThing([
    'thingName' => $thingName,
]);
$result = $awsIoTClient->createKeysAndCertificate([
    'setAsActive' => TRUE,
]);
$certArn = $result['certificateArn'];
$certId = $result['certificateId'];

$certPem = $result['certificatePem'];
$privateKey = $result['keyPair']['PrivateKey'];
$awsIoTClient->attachPrincipalPolicy([
        'policyName' => $policyName,
        'principal' => $certArn
]);
$awsIoTClient->attachThingPrincipal([
        'principal' => $certArn,
        'thingName' => $thingName
]);

上面的代码成功创建了一个东西。我可以看到运行时创建的东西:

$awsIoTClient->listThings();

然后,当我尝试使用以下代码访问事物的影子时:

Use Aws\IotDataPlane\IotDataPlaneClient;
$client = new IotDataPlaneClient([
    'version' => 'latest',
    'region' => <region>,
    'credentials' => [
      'key'    => <aws_access_key>,
      'secret' => <aws_secret_key>,
    ]
]);
$result = $client->getThingShadow([
  'thingName' => '<string uuid>', // REQUIRED
]);

我收到以下错误:

Aws\IotDataPlane\Exception\IotDataPlaneException: Error executing "GetThingShadow" on "https://data.iot.us-east-1.amazonaws.com/things/<string uuid>/shadow"; AWS HTTP error: Client error: 404 ResourceNotFoundException (client): No shadow exists with name: '<string uuid>' - {"message":"No shadow exists with name: '<string uuid>'","traceId":"<traceId>"} in Aws\WrappedHttpHandler->parseError() (line 152 of /<docroot>/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php).

需要注意的几件事:使用其访问和密钥创建此事物的用户具有以下 AWS 策略(一旦我们开始工作,我们将锁定这些策略):

- AWSIoTLogging
- AWSIoTConfigAccess
- AWSIoTRuleActions
- AWSIoTConfigReadOnlyAccess
- AWSIoTDataAccess
- AWSIoTFullAccess
4

1 回答 1

4

原来答案是您需要先更新阴影才能阅读它。

$json = json_encode(['state' => ['desired' => ['test_updated' => "date updated " . date('r')]]]);
$result = $client->getThingShadow([
  'thingName' => $thingname,
  'payload' => $json,
]);

之后,这将返回一个影子:

$result = $client->getThingShadow([
  'thingName' => '<string uuid>', // REQUIRED
]);
于 2015-11-23T13:07:05.227 回答