HTTPS - AWS IoT Core

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

HTTPS

用戶端可以透過向RESTAPI使用 HTTP 1.0 或 1.1 通訊協定發出要求來發佈訊息。如需HTTP要請求所使用的驗證和連接埠對應,請參閱通訊協定、連接埠映射和身分驗證

注意

HTTPS不支持像這MQTT樣的clientId值。 clientId使用時可用MQTT,但使用時不可用HTTPS。

HTTPS消息 URL

裝置和用戶端會向用戶端特定端點和特定主題URL發出POST要求,以發佈其訊息:

https://IoT_data_endpoint/topics/url_encoded_topic_name?qos=1
  • IoT_data_endpointAWS IoT 裝置資料端點。您可以使用以下 AWS CLI 指令,在物件的詳細資訊頁面或用戶端上尋找 AWS IoT 主控台中的端點:

    aws iot describe-endpoint --endpoint-type iot:Data-ATS

    此端點看起來如下:a3qjEXAMPLEffp-ats.iot.us-west-2.amazonaws.com

  • url_encoded_topic_name 是要傳送之郵件的完整主題名稱

HTTPS訊息程式碼範例

以下是一些如何將HTTPS訊息傳送至的範例 AWS IoT。

Python (port 8443)
import requests import argparse # define command-line parameters parser = argparse.ArgumentParser(description="Send messages through an HTTPS connection.") parser.add_argument('--endpoint', required=True, help="Your AWS IoT data custom endpoint, not including a port. " + "Ex: \"abcdEXAMPLExyz-ats.iot.us-east-1.amazonaws.com\"") parser.add_argument('--cert', required=True, help="File path to your client certificate, in PEM format.") parser.add_argument('--key', required=True, help="File path to your private key, in PEM format.") parser.add_argument('--topic', required=True, default="test/topic", help="Topic to publish messages to.") parser.add_argument('--message', default="Hello World!", help="Message to publish. " + "Specify empty string to publish nothing.") # parse and load command-line parameter values args = parser.parse_args() # create and format values for HTTPS request publish_url = 'https://' + args.endpoint + ':8443/topics/' + args.topic + '?qos=1' publish_msg = args.message.encode('utf-8') # make request publish = requests.request('POST', publish_url, data=publish_msg, cert=[args.cert, args.key]) # print results print("Response status: ", str(publish.status_code)) if publish.status_code == 200: print("Response body:", publish.text)
Python (port 443)
import requests import http.client import json import ssl ssl_context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT) ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2 # note the use of ALPN ssl_context.set_alpn_protocols(["x-amzn-http-ca"]) ssl_context.load_verify_locations(cafile="./<root_certificate>") # update the certificate and the AWS endpoint ssl_context.load_cert_chain("./<certificate_in_PEM_Format>", "<private_key_in_PEM_format>") connection = http.client.HTTPSConnection('<the ats IoT endpoint>', 443, context=ssl_context) message = {'data': 'Hello, I'm using TLS Client authentication!'} json_data = json.dumps(message) connection.request('POST', '/topics/device%2Fmessage?qos=1', json_data) # make request response = connection.getresponse() # print results print(response.read().decode())
CURL

您可以使用 curl 從用戶端或裝置傳送訊息至 AWS IoT。

使用 curl 從用 AWS IoT 戶端裝置傳送訊息
  1. 檢查 curl 版本。

    1. 在您的用戶端上,請在命令提示中執行此命令。

      curl --help

      在說明文字中,尋找選TLS項。您應該看到 --tlsv1.2 選項。

    2. 如果您看到 --tlsv1.2 選項,請繼續。

    3. 如果您沒有看到 --tlsv1.2 選項或您收到 command not found 錯誤訊息,您可能需要在用戶端上更新或安裝 curl 或安裝 openssl,然後才能繼續進行。

  2. 在用戶端上安裝憑證。

    複製您在 AWS IoT 主控台中註冊用戶端 (物件) 時建立的憑證檔案。在繼續之前,請確定您的用戶端上有這三個憑證檔案。

    • CA 憑證檔案 (Amazon-root-CA-1.pem 在這個例子中)。

    • 用戶端的憑證檔案 (device.pem.crt 在這個例子中)。

    • 用戶端的私密金鑰檔案 (private.pem.key 在這個例子中)。

  3. 建立 curl 命令列,取代您帳戶和系統的可取代值。

    curl --tlsv1.2 \ --cacert Amazon-root-CA-1.pem \ --cert device.pem.crt \ --key private.pem.key \ --request POST \ --data "{ \"message\": \"Hello, world\" }" \ "https://IoT_data_endpoint:8443/topics/topic?qos=1"
    --tlsv1.2

    使用 TLS 1.2 (SSL)。

    -卡 Amazon-root-CA-1.pem

    驗證對等的憑證授權機構憑證檔案名稱和路徑 (如有必要)。

    --cert device.pem.crt

    用戶端的憑證檔案名稱和路徑 (如有必要)。

    -鍵 private.pem.key

    用戶端的私密金鑰檔案名稱和路徑 (如有必要)。

    -請求 POST

    HTTP要求的類型 (在本例中為POST)。

    -數據」{ \"message\": \"Hello, world\" }"

    您要發佈的HTTPPOST資料。在這種情況下,它是一個JSON字符串,內部引號用反斜杠字符(\)轉義。

    「HTTPS://IoT_data_endpoint:844 /主題/主題/topic? QOS = 1」

    用戶端URL的 AWS IoT 裝置資料端點,後面接著HTTPS連接埠:8443,接著接著是關鍵字/topics/和主題名稱 (在這種情況下)。topic指定服務品質作為查詢參數 ?qos=1

  4. 在控制台中打開MQTT測試客戶 AWS IoT 端。

    按照中的說明使用 MQTT 用戶端檢視 MQTT 訊息 AWS IoT進行操作,將控制台設定為訂閱主題名稱為 topiccurl指令中使用,或使用的萬用字元主題篩選器#

  5. 測試命令。

    監控 AWS IoT 主控台測試用戶端中的主題時,請前往您的用戶端並發出您在步驟 3 中建立的 curl 命令列。您應該會在主控台中看到用戶端的訊息。