文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS IoT 使用 AWS SDKs 的程式碼範例
下列程式碼範例示範如何使用 AWS 軟體開發套件 AWS IoT (SDK)。
基本概念是程式碼範例,示範如何在服務內執行基本操作。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在相關案例中查看內容中的動作。
其他 資源
AWS IoT 開發人員指南 – 有關 的詳細資訊 AWS IoT。
AWS IoT API參考 – 所有可用 AWS IoT 動作的詳細資訊。
AWS 開發人員中心
– 您可以依類別或全文搜尋篩選的程式碼範例。 AWS SDK範例
– 使用偏好語言的完整程式碼的 GitHub 儲存庫。包含設定和執行程式碼的指示。
開始使用
下列程式碼範例示範如何開始使用 AWS IoT。
- C++
-
- C++ 的 SDK
-
CMakeLists.txt CMake 檔案的程式碼。
# Set the minimum required version of CMake for this project. cmake_minimum_required(VERSION 3.13) # Set the AWS service components used by this project. set(SERVICE_COMPONENTS iot) # Set this project's name. project("hello_iot") # Set the C++ standard to use to build this target. # At least C++ 11 is required for the AWS SDK for C++. set(CMAKE_CXX_STANDARD 11) # Use the MSVC variable to determine if this is a Windows build. set(WINDOWS_BUILD ${MSVC}) if (WINDOWS_BUILD) # Set the location where CMake can find the installed libraries for the AWS SDK. string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all") list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH}) endif () # Find the AWS SDK for C++ package. find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS}) if (WINDOWS_BUILD AND AWSSDK_INSTALL_AS_SHARED_LIBS) # Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging. # set(BIN_SUB_DIR "/Debug") # If you are building from the command line, you may need to uncomment this # and set the proper subdirectory to the executables' location. AWSSDK_CPY_DYN_LIBS(SERVICE_COMPONENTS "" ${CMAKE_CURRENT_BINARY_DIR}${BIN_SUB_DIR}) endif () add_executable(${PROJECT_NAME} hello_iot.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})
hello_iot.cpp 來源檔案的程式碼。
#include <aws/core/Aws.h> #include <aws/iot/IoTClient.h> #include <aws/iot/model/ListThingsRequest.h> #include <iostream> /* * A "Hello IoT" starter application which initializes an AWS IoT client and * lists the AWS IoT topics in the current account. * * main function * * Usage: 'hello_iot' * */ int main(int argc, char **argv) { Aws::SDKOptions options; // Optional: change the log level for debugging. // options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug; Aws::InitAPI(options); // Should only be called once. { Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::IoT::IoTClient iotClient(clientConfig); // List the things in the current account. Aws::IoT::Model::ListThingsRequest listThingsRequest; Aws::String nextToken; // Used for pagination. Aws::Vector<Aws::IoT::Model::ThingAttribute> allThings; do { if (!nextToken.empty()) { listThingsRequest.SetNextToken(nextToken); } Aws::IoT::Model::ListThingsOutcome listThingsOutcome = iotClient.ListThings( listThingsRequest); if (listThingsOutcome.IsSuccess()) { const Aws::Vector<Aws::IoT::Model::ThingAttribute> &things = listThingsOutcome.GetResult().GetThings(); allThings.insert(allThings.end(), things.begin(), things.end()); nextToken = listThingsOutcome.GetResult().GetNextToken(); } else { std::cerr << "List things failed" << listThingsOutcome.GetError().GetMessage() << std::endl; break; } } while (!nextToken.empty()); std::cout << allThings.size() << " thing(s) found." << std::endl; for (auto const &thing: allThings) { std::cout << thing.GetThingName() << std::endl; } } Aws::ShutdownAPI(options); // Should only be called once. return 0; }
-
如需 API 詳細資訊,請參閱 listThings AWS SDK for C++ 參考中的 API。
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 -
- Java
-
- Java 2.x 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.iot.IotClient; import software.amazon.awssdk.services.iot.model.ListThingsRequest; import software.amazon.awssdk.services.iot.model.ListThingsResponse; import software.amazon.awssdk.services.iot.model.ThingAttribute; import software.amazon.awssdk.services.iot.paginators.ListThingsIterable; import java.util.List; public class HelloIoT { public static void main(String[] args) { System.out.println("Hello AWS IoT. Here is a listing of your AWS IoT Things:"); IotClient iotClient = IotClient.builder() .region(Region.US_EAST_1) .build(); listAllThings(iotClient); } public static void listAllThings(IotClient iotClient) { iotClient.listThingsPaginator(ListThingsRequest.builder() .maxResults(10) .build()) .stream() .flatMap(response -> response.things().stream()) .forEach(attribute -> { System.out.println("Thing name: " + attribute.thingName()); System.out.println("Thing ARN: " + attribute.thingArn()); }); } }
-
如需 API 詳細資訊,請參閱 listThings AWS SDK for Java 2.x 參考中的 API。
-
- Kotlin
-
- SDK for Kotlin
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import aws.sdk.kotlin.services.iot.IotClient import aws.sdk.kotlin.services.iot.model.ListThingsRequest suspend fun main() { println("A listing of your AWS IoT Things:") listAllThings() } suspend fun listAllThings() { val thingsRequest = ListThingsRequest { maxResults = 10 } IotClient { region = "us-east-1" }.use { iotClient -> val response = iotClient.listThings(thingsRequest) val thingList = response.things if (thingList != null) { for (attribute in thingList) { println("Thing name ${attribute.thingName}") println("Thing ARN: ${attribute.thingArn}") } } } }
-
如需 API 詳細資訊,請參閱 listThings
AWS for Kotlin SDK 參考中的 API。
-