Example 3: Upload files to your OSS space using MQTT

Updated at:

The file upload feature of IoT Platform stores your uploaded files in the platform's OSS space. You can also use MQTT to upload files to your own OSS space.

Note

After you upload a file to your own OSS space, you can manage the file only in your OSS space. You cannot manage the file on the Device Details > File Management page in the IoT Platform console.

Prerequisites

  • Grant the device permission to perform operations on OSS in the IoT Platform console. For more information, see Configure file uploads.

  • Obtain the configuration information for the extra_params parameter from the file upload page. For more information, see File upload.

    Note

    The extra_params parameter is required to upload files to your own OSS space. If you are not uploading to your own OSS space, set this parameter to NULL.

Procedure

  1. Initialize the MQTT module.

  2. Initialize the MQTT file upload module. In the mqtt_upload_user_oss_demo.c file, specify the information for the file to upload.

    Note

    The extra_params parameter supports only the JSON format. For more information, see File upload.

    Example of the extra_params format:

    {
        "ossOwnerType":"device-user",
        "serviceId":"1234567890",
        "fileTag":{
            "tagKey1":"tagValue1",
            "tagKey2":"tagValue2"
        }
    }

    Example of parameter configuration for the upload component:

        uint32_t rsp_timeout = 10000;
        aiot_mqtt_upload_setopt(up_handle, AIOT_MQTT_UPLOADOPT_RSP_TIMEOUT_MS, &rsp_timeout);
        uint32_t rety_count = 5;
        aiot_mqtt_upload_setopt(up_handle, AIOT_MQTT_UPLOADOPT_RETRY_COUNT, &rety_count);
        uint32_t block_size = 256;
        aiot_mqtt_upload_setopt(up_handle, AIOT_MQTT_UPLOADOPT_DEFAULT_BLOCK_SIZE, &block_size);
    
        
        /* Configure the information of the file to upload over MQTT. If the file size is unknown, set the file length to -1 and the read_data_handler callback function to NULL.
           Use the aiot_mqtt_upload_send_data function to send data. */
        uint32_t file_size = -1;
        
        /* To upload the file to your OSS space, configure the extra_params parameter.
           If you do not need to upload the file to your OSS space, set extra_params to NULL.
           You can then view the file on the File Management page.
           [The extra_params parameter must be in JSON format.]
         */
        char *extra_params = "{\"ossOwnerType\":\"device-user\",\"serviceId\":\"1234567890\",\"fileTag\":{\"tagKey1\":\"tagValue1\",\"tagKey2\":\"tagValue2\"}}";
        
        aiot_mqtt_upload_file_opt_t file_option = {
            .file_name = MQTT_UPLOAD_SINGLE_FILE_NAME,
            .file_size = file_size,
            .mode = AIOT_MQTT_UPLOAD_FILE_MODE_OVERWRITE,
            .digest = NULL,
            .read_data_handler = NULL,
            .userdata = NULL,
            .extra_params = extra_params
        };
    
        res = aiot_mqtt_upload_setopt(up_handle, AIOT_MQTT_UPLOADOPT_FILE_OPTION, &file_option);
     
  3. Request the file upload.

    The aiot_mqtt_upload_open_stream function for requesting a file upload is a synchronous function. You must wait for an acknowledgement before you can start the upload.

    aiot_mqtt_upload_recv_t recv_packet = {0};
        res = aiot_mqtt_upload_open_stream(up_handle, MQTT_UPLOAD_SINGLE_FILE_NAME, &recv_packet);
        if (res < STATE_SUCCESS) {
            goto exit;
        }
  4. Upload file shards.

    Call the aiot_mqtt_upload_send_data function to upload file shards.

    Note

    The is_complete parameter notifies IoT Platform whether a packet is the last one for the file upload. During the upload, set this parameter to 0. When you upload the last packet, set is_complete to 1.

  5. if ((upload_file_len + data_len) < MQTT_UPLOAD_SINGLE_FILE_SIZE) {
        /* Send a data packet. */
        data_len = block_size;
        is_commplete = 0;
    } else {
        /* Send the last packet of the file. */
        data_len = MQTT_UPLOAD_SINGLE_FILE_SIZE - upload_file_len;
        is_commplete = 1;
    }
    
    res = aiot_mqtt_upload_send_data(up_handle, MQTT_UPLOAD_SINGLE_FILE_NAME,            &g_upload_single_file_buf[upload_file_len], data_len, is_commplete);
    if (res == STATE_SUCCESS) {
        upload_file_len += data_len;
        printf("upload_file_len:%d\r\n", upload_file_len);
    } 
  6. After the file is uploaded, destroy the session handle to release resources.

    /* After all files are uploaded, destroy the MQTT upload session instance. */
    aiot_mqtt_upload_deinit(&up_handle);

Log description

  1. The MQTT connection is successful.

    [1645604145.783][LK-0313] MQTT user calls aiot_mqtt_connect api, connect
    [1645604145.783][LK-032A] mqtt host: a1******Dih.iot-as-mqtt.cn-shanghai.aliyuncs.com
    [1645604145.783][LK-0317] user name: mqttupload0002&a1******Dih
    [1645604145.783][LK-0318] password: 194163B399BA079B9BB6*******************F2D275A9D9ADFBA29
    success to establish tcp, fd=5
    local port: 51712
    [1645604145.862][LK-1000] establish mbedtls connection with server(host='a1******Dih.iot-as-mqtt.cn-shanghai.aliyuncs.com', port=[443])
    [1645604145.889][LK-1000] success to establish mbedtls connection, (cost 45260 bytes in total, max used 47996 bytes)
    [1645604146.138][LK-0313] MQTT connect success in 355 ms
    AIOT_MQTTEVT_CONNECT
  2. An MQTT file upload session is created and the client subscribes to the relevant topics.

    [1645604146.138][LK-2113] aiot_mqtt_upload_init init
    [1645604146.138][LK-0309] sub: /sys/a1******Dih/mqttupload****/thing/file/upload/mqtt/init_reply
    [1645604146.138][LK-0309] sub: /sys/a1******Dih/mqttupload****/thing/file/upload/mqtt/send_reply
    [1645604146.138][LK-0309] sub: /sys/a1******Dih/mqttupload****/thing/file/upload/mqtt/cancel_reply
  3. A request is sent to upload a file to your OSS space.

    [1645671153.383][LK-0309] pub: /sys/a1******Dih/mqttupload****/thing/file/upload/mqtt/init
    
    [LK-030A] > 7B 22 69 64 22 3A 22 31  22 2C 22 70 61 72 61 6D | {"id":"1","param
    [LK-030A] > 73 22 3A 7B 22 66 69 6C  65 4E 61 6D 65 22 3A 22 | s":{"fileName":"
    [LK-030A] > 6D 71 74 74 75 70 6C 6F  61 64 66 69 6C 65 2E 74 | mqttuploadfile.t
    [LK-030A] > 78 74 22 2C 22 66 69 6C  65 53 69 7A 65 22 3A 2D | xt","fileSize":-
    [LK-030A] > 31 2C 22 63 6F 6E 66 6C  69 63 74 53 74 72 61 74 | 1,"conflictStart
    [LK-030A] > 65 67 79 22 3A 22 6F 76  65 72 77 72 69 74 65 22 | egy":"overwrite"
    [LK-030A] > 2C 22 69 6E 69 74 55 69  64 22 3A 22 31 36 34 35 | ,"initUid":"1645
    [LK-030A] > 36 ** ** *1 35 33 33 38  33 39 22 2C 22 65 78 74 | 671*****39","ext
    [LK-030A] > 72 61 50 61 72 61 6D 73  22 3A 7B 22 6F 73 73 4F | raParams":{"ossO
    [LK-030A] > 77 6E 65 72 54 79 70 65  22 3A 22 64 65 76 69 63 | wnerType":"devic
    [LK-030A] > 65 2D 75 73 65 72 22 2C  22 73 65 72 76 69 63 65 | e-user","service
    [LK-030A] > 49 64 22 3A 22 31 32 33  34 35 36 37 38 39 30 22 | Id":"1234567890"
    [LK-030A] > 2C 22 66 69 6C 65 74  61 67 22 3A 7B 22 74 61 67 | ,"fileTag":{"tag
    [LK-030A] > 4B 65 79 31 22 3A 22  74 61 67 56 61 6C 75 65 31 | Key1":"tagValue1
    [LK-030A] > 22 2C 22 74 61 67 4B  65 79 32 22 3A 22 74 61 67 | ","tagKey2":"tag
    [LK-030A] > 56 61 6C 75 65 32 22  7D 7D 7D 7D                | Value2"}
  4. An acknowledgement message is received from IoT Platform.

    [1645604146.391][LK-0309] pub: /sys/a1******Dih/mqttupload****/thing/file/upload/mqtt/init_reply
    
    [LK-030A] < 7B 22 63 6F 64 65 22 3A  32 30 30 2C 22 64 61 74 | {"code":200,"dat
    [LK-030A] < 61 22 3A 7B 22 66 69 6C  65 4E 61 6D 65 22 3A 22 | a":{"fileName":"
    [LK-030A] < 6D 71 74 74 75 70 6C 6F  61 64 66 69 6C 65 2E 74 | mqttuploadfile.t
    [LK-030A] < 78 74 22 2C 22 75 70 6C  6F 61 64 49 64 22 3A 22 | xt","uploadId":"
    [LK-030A] < 66 66 35 4B 49 38 ** **  ** ** ** 62 63 51 69 52 | ff5KI8*****bcQiR
    [LK-030A] < 75 43 35 36 30 31 30 32  30 30 22 2C 22 6F 66 66 | uC56010200","off
    [LK-030A] < 73 65 74 22 3A 30 2C 22  66 69 6C 65 53 69 7A 65 | set":0,"fileSize
    [LK-030A] < 22 3A 2D 31 2C 22 63 6F  6E 66 6C 69 63 74 53 74 | ":-1,"conflictSt
    [LK-030A] < 72 61 74 65 67 79 22 3A  22 6F 76 65 72 77 72 69 | rategy":"overwri
    [LK-030A] < 74 65 22 7D 2C 22 69 64  22 3A 22 31 22 2C 22 6D | te"},"id":"1","m
    [LK-030A] < 65 73 73 61 67 65 22 3A  22 73 75 63 63 65 73 73 | essage":"success
    [LK-030A] < 22 7D                                            | "}
  5. Send file shards.

    The aiot_mqtt_upload_send_data function is called recursively to upload data. This is a synchronous function. You must wait for a STATE_SUCCESS acknowledgement before you can upload the next data packet.

    [1645604146.391][LK-210B] Recv file name:mqttuploadfile.txt
    [1645604146.401][LK-0309] pub: /sys/a1******Dih/mqttupload****/thing/file/upload/mqtt/send
    
    [LK-030A] > 00 67 7B 22 69 64 22 3A  22 32 22 2C 22 70 61 72 | .g{"id":"2","par
    [LK-030A] > 61 6D 73 22 3A 7B 22 75  70 6C 6F 61 64 49 64 22 | ams":{"uploadId"
    [LK-030A] > 3A 22 66 66 35 4B 49 **  ** ** ** ** ** ** 63 51 | :"ff5KI********Q
    [LK-030A] > 69 52 75 43 35 36 30 31  30 32 30 30 22 2C 22 6F | iRuC56010200","o
    [LK-030A] > 66 66 73 65 74 22 3A 30  2C 22 62 53 69 7A 65 22 | ffset":0,"bSize"
    [LK-030A] > 3A 32 35 36 2C 22 69 73  43 6F 6D 70 6C 65 74 65 | :256,"isComplete
    [LK-030A] > 22 3A 66 61 6C 73 65 7D  7D 00 00 00 00 00 00 00 | ":false}}.......
    [LK-030A] > 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 | ................
    [LK-030A] > 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 | ................
    [LK-030A] > 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 | ................
    [LK-030A] > 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 | ................
  6. Finish the file upload.

    When you upload the last data packet, you must set isComplete to 1 to notify IoT Platform that the file upload is complete.

    [LK-030A] < 7B 22 63 6F 64 65 22 3A  32 30 30 2C 22 64 61 74 | {"code":200,"dat
    [LK-030A] < 61 22 3A 7B 22 66 69 63  4D 6F 64 65 22 3A 22 63 | a":{"ficMode":"c
    [LK-030A] < 72 63 36 34 22 2C 22 75  70 6C 6F 61 64 49 64 22 | rc64","uploadId"
    [LK-030A] < 3A 22 66 66 35 4B 49 **  ** ** ** ** ** ** ** 51 | :"ff5KI*******Q
    [LK-030A] < 69 52 75 43 35 36 30 31  30 32 30 30 22 2C 22 6F | iRuC56010200","o
    [LK-030A] < 66 66 73 65 74 22 3A 37  36 38 2C 22 63 6F 6D 70 | ffset":768,"comp
    [LK-030A] < 6C 65 74 65 22 3A 74 72  75 65 2C 22 66 69 63 56 | lete":true,"ficV
    [LK-030A] < 61 6C 75 65 53 65 72 76  65 72 22 3A 22 63 33 37 | alueServer":"c37
    [LK-030A] < 38 36 33 39 37 32 30 36  39 32 37 30 63 22 2C 22 | 863********270c","
    [LK-030A] < 62 53 69 7A 65 22 3A 32  35 36 7D 2C 22 69 64 22 | bSize":256},"id"
    [LK-030A] < 3A 22 35 22 2C 22 6D 65  73 73 61 67 65 22 3A 22 | :"5","message":"
    [LK-030A] < 73 75 63 63 65 73 73 22  7D                      | success"}
    
    [1645604147.904][LK-2125] Complete upload total size:1024
    upload_file_len:1024
    [1645604147.908][LK-2100] Finish upload,Deleted upload task.
    MQTT Upload file(mqttuploadfile.txt) uploadid(ff5KI*********RuC56010200) success.
  7. After the upload is complete, destroy the handle to release resources.

    [1645604152.908][LK-210F] aiot_mqtt_upload_deinit
    [1645604152.908][LK-0309] unsub: /sys/a1******Dih/mqttupload****/thing/file/upload/mqtt/init_reply
    [1645604152.908][LK-0309] unsub: /sys/a1******Dih/mqttupload****/thing/file/upload/mqtt/send_reply
    [1645604152.908][LK-0309] unsub: /sys/a1******Dih/mqttupload****/thing/file/upload/mqtt/cancel_reply
    [1645604152.908][LK-1000] adapter_network_deinit