的版本 4 (V4) 适用于 .NET 的 AWS SDK 已经发布!
有关重大更改和迁移应用程序的信息,请参阅迁移主题。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
AWS IoT data 使用 适用于 .NET 的 SDK (v4) 的示例
以下代码示例向您展示了如何通过使用 适用于 .NET 的 AWS SDK (v4) 来执行操作和实现常见场景。 AWS IoT data
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例演示了如何使用 GetThingShadow。
- 适用于 .NET 的 SDK (v4)
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /// <summary> /// Gets the Thing's shadow information. /// </summary> /// <param name="thingName">The name of the Thing.</param> /// <returns>The shadow data as a string, or null if retrieval failed.</returns> public async Task<string?> GetThingShadowAsync(string thingName) { try { var request = new GetThingShadowRequest { ThingName = thingName }; var response = await _amazonIotData.GetThingShadowAsync(request); using var reader = new StreamReader(response.Payload); var shadowData = await reader.ReadToEndAsync(); _logger.LogInformation($"Retrieved shadow for Thing {thingName}"); return shadowData; } catch (Amazon.IotData.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot get Thing shadow - resource not found: {ex.Message}"); return null; } catch (Exception ex) { _logger.LogError($"Couldn't get Thing shadow. Here's why: {ex.Message}"); return null; } }-
有关 API 的详细信息,请参阅 适用于 .NET 的 AWS SDK API 参考GetThingShadow中的。
-
以下代码示例演示了如何使用 UpdateThingShadow。
- 适用于 .NET 的 SDK (v4)
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /// <summary> /// Updates the Thing's shadow with new state information. /// </summary> /// <param name="thingName">The name of the Thing.</param> /// <param name="shadowPayload">The shadow payload in JSON format.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> UpdateThingShadowAsync(string thingName, string shadowPayload) { try { var request = new UpdateThingShadowRequest { ThingName = thingName, Payload = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(shadowPayload)) }; await _amazonIotData.UpdateThingShadowAsync(request); _logger.LogInformation($"Updated shadow for Thing {thingName}"); return true; } catch (Amazon.IotData.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot update Thing shadow - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't update Thing shadow. Here's why: {ex.Message}"); return false; } }-
有关 API 的详细信息,请参阅 适用于 .NET 的 AWS SDK API 参考UpdateThingShadow中的。
-