与 AWS SDK或CreateGateway一起使用 CLI - AWS SDK 代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或CreateGateway一起使用 CLI

以下代码示例演示如何使用 CreateGateway

CLI
AWS CLI

创建网关

以下create-gateway示例创建了一个在 IoT Greengrass 上运行的 AWS 网关。

aws iotsitewise create-gateway \ --gateway-name ExampleCorpGateway \ --gateway-platform greengrass={groupArn=arn:aws:greengrass:us-west-2:123456789012:/greengrass/groups/a1b2c3d4-5678-90ab-cdef-1b1b1EXAMPLE}

输出:

{ "gatewayId": "a1b2c3d4-5678-90ab-cdef-1a1a1EXAMPLE", "gatewayArn": "arn:aws:iotsitewise:us-west-2:123456789012:gateway/a1b2c3d4-5678-90ab-cdef-1a1a1EXAMPLE" }

有关更多信息,请参阅《AWS 物联网 SiteWise 用户指南》中的配置网关。

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

/** * Creates a new IoT Sitewise gateway. * * @param gatewayName The name of the gateway to create. * @param myThing The name of the core device thing to associate with the gateway. * @return a {@link CompletableFuture} that represents a {@link String} result of the gateways ID. The calling code * can attach callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<String> createGatewayAsync(String gatewayName, String myThing) { GreengrassV2 gg = GreengrassV2.builder() .coreDeviceThingName(myThing) .build(); GatewayPlatform platform = GatewayPlatform.builder() .greengrassV2(gg) .build(); Map<String, String> tag = new HashMap<>(); tag.put("Environment", "Production"); CreateGatewayRequest createGatewayRequest = CreateGatewayRequest.builder() .gatewayName(gatewayName) .gatewayPlatform(platform) .tags(tag) .build(); return getAsyncClient().createGateway(createGatewayRequest) .handle((response, exception) -> { if (exception != null) { logger.error("Error creating the gateway."); throw (CompletionException) exception; } logger.info("The ARN of the gateway is {}" , response.gatewayArn()); return response.gatewayId(); }); }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 CreateGateway” 中的。

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class IoTSitewiseWrapper: """Encapsulates AWS IoT SiteWise actions using the client interface.""" def __init__(self, iotsitewise_client: client) -> None: """ Initializes the IoTSitewiseWrapper with an AWS IoT SiteWise client. :param iotsitewise_client: A Boto3 AWS IoT SiteWise client. This client provides low-level access to AWS IoT SiteWise services. """ self.iotsitewise_client = iotsitewise_client self.entry_id = 0 # Incremented to generate unique entry IDs for batch_put_asset_property_value. @classmethod def from_client(cls) -> "IoTSitewiseWrapper": """ Creates an IoTSitewiseWrapper instance with a default AWS IoT SiteWise client. :return: An instance of IoTSitewiseWrapper initialized with the default AWS IoT SiteWise client. """ iotsitewise_client = boto3.client("iotsitewise") return cls(iotsitewise_client) def create_gateway(self, gateway_name: str, my_thing: str) -> str: """ Creates an AWS IoT SiteWise Gateway. :param gateway_name: The name of the gateway to create. :param my_thing: The core device thing name. :return: The ID of the created gateway. """ try: response = self.iotsitewise_client.create_gateway( gatewayName=gateway_name, gatewayPlatform={ "greengrassV2": {"coreDeviceThingName": my_thing}, }, tags={"Environment": "Production"}, ) gateway_id = response["gatewayId"] return gateway_id except ClientError as err: if err.response["Error"]["Code"] == "ResourceAlreadyExistsException": logger.error("Gateway %s already exists.", gateway_name) else: logger.error( "Error creating gateway %s. Here's why %s", gateway_name, err.response["Error"]["Message"], ) raise
  • 有关API详细信息,请参阅CreateGateway中的 AWS SDKPython (Boto3) API 参考。