Hello @Alex Fang,
you are currently using a C2D message which is part of the IoT Hub cloud-to-device communication strategies:
- DeviceTwin; send desired properties and read reported properties. The device does not have to be connected at that moment. It can read and write properties when it connects to the cloud at a later moment
- Direct method; Send a message body and receive a response body once the method is executed on the device. The device must be connected to the cloud while the method call is made. A method can time-out.
- Cloud messages; queue one or more cloud messages which can be picked up by a device once it connects to the cloud. Each message must be accepted to be taken from the queue. Cloud messages do not return a response (eg. use reported properties on the device twin)
As you can see, sending a file could be hard using one of these solutions due to message size restrictions.
Still, your question is a common one and has there is a common pattern to solve this.
Put the files you want to send to a device in a blob storage and send the device a desired property with the SAS token so the device can download it over HTTPS:
SAS tokens can expire so you could set the expiration date far into the future.
A more secure solution is when a device has no access to the file anymore (SAS is expired), It sends a custom message to the cloud requesting a new SAS. Then, some cloud logic (like an Azure Function) creates a new SAS token and updates the desired property.
This is the most flexible way to download small or large messages in a secure way.
If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.