Sending a File to Device Cloud in Python

A client application can send a file from any location on the device to which it has read access and add it to the global file storage or the file storage of a thing in Device Cloud.

As part of the file transfer function call, the application can specify optional parameters as follows:

  • a callback function to call after the file transfer completes successfully to perform any post-processing

  • whether to wait for the file transfer to finish and how long to wait, in seconds (by default, the application does not block and the timeout is zero)

To send files, the application must be connected to Device Cloud.

The application must have read access to the specified file and directory.

  1. (Optional) Implement a callback function to run when the file upload completes successfully.

    When the function is called, it receives the file_name and status parameters.

    def file_upload_callback(client, file_name, status):
        # Perform any post-processing actions as needed
        return(iot.STATUS_SUCCESS) 
  2. Call the file upload API.

    If you did not implement a callback, specify None for the callback parameter.

    If you want to use the same file name in Device Cloud, specify None for the upload_name parameter.

    client.file_upload(file_path="/path/to/my_file", upload_name="my_cloud_file_name", \
    callback=file_upload_callback, blocking=True, timeout=60, file_global=True)

When the application calls the function, the file appears in Device Cloud in the storage location as specified in the file_global parameter. If a file with the same name exists in the storage location, it is overwritten.