Azure IoT 支持通过 MQTT 或 AMQP 等多种协议发送数据,但在某些情况下消息体积过大,需要将文件上传至 Azure 服务器。为解决此问题,IoT 中心通过为已连接设备提供预配置 Blob 容器和 Azure 存储账户的共享访问签名(SAS)URI(每次上传一个),来促进文件上传。
从 sgcWebSockets 2023.8.0 起,您可以使用 Azure IoT 客户端上传文件,证书和 SAS 均为受支持的身份验证方法。
您可以通过以下链接了解更多关于文件上传的信息:
https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-file-upload
使用证书上传文件
首先在 Azure 账户中配置证书,然后在 Azure IoT 客户端组件中设置证书和密钥文件的路径。调用 UploadFile 方法通过文件名路径上传文件。
oClient := TsgcIoTAzure_MQTT_Client.Create(nil);
Try
oClient.Certificate.CertFile := 'cert.pem';
oClient.Certificate.KeyFile := 'key.pem';
oClient.Certificate.Enabled := True;
oDialog := TOpenDialog.Create(nil);
Try
if oDialog.Execute then
oClient.UploadFile(oDialog.FileName);
Finally
oDialog.Free;
End;
Finally
FreeAndNil(oClient);
End;
使用 SAS 上传文件
首先在 Azure IoT 客户端组件中配置 Secret Key。调用 UploadFile 方法通过文件名路径上传文件。
oClient := TsgcIoTAzure_MQTT_Client.Create(nil);
Try
oClient.SAS.SecretKey := 'your-secret-key';
oClient.SAS.KeyName := 'key-name';
oClient.SAS.Enabled := True;
oDialog := TOpenDialog.Create(nil);
Try
if oDialog.Execute then
oClient.UploadFile(oDialog.FileName);
Finally
oDialog.Free;
End;
Finally
FreeAndNil(oClient);
End;
