Download File

下载单个资产的文件。

操作权限

需授权的资源

所需操作权限

资产

Read

请求格式

GET https://{integration-address}/connect-service/v2.1/files?action=download

备注

{integration-address}:消息集成服务网关地址。该信息可登陆 EnOS 管理控制台 ,在 帮助 > 环境信息 > HTTP消息集成通道 中获取。

请求参数(URI)

备注

必须在请求参数中使用以下任意一个参数或参数组合以指定资产:

  • assetId 以指定一个设备或一个逻辑资产

  • productKey + deviceKey 以指定一个设备

名称

位置(Path/Query)

必需/可选

数据类型

描述

orgId

Query

必需

String

资产所属的组织ID。如何获取orgId信息>>

fileUri

Query

必需

String

文件下载uri,格式:enos-connect://xxxx 。

category

Query

必需

String

需要下载的文件类别。模型功能相关的文件,如属性、测点、事件,值为 feature ;OTA资源包,值为 ota

assetId

Query

可选

String

资产ID。

productKey

Query

可选

String

设备资产的product key。

deviceKey

Query

可选

String

设备资产的device key。

响应参数

名称

数据类型

描述

文件的二进制数据流

output stream

返回文件输出流。

示例

请求示例

url: https://{integration-address}/connect-service/v2.1/files?action=download&orgId=yourOrgId&fileUri=yourFileUri&assetId=yourAssetId&category=feature
method: GET
requestHeader: {"apim-accesstoken":"yourAccessToken"}

Java SDK调用示例

import com.envisioniot.enos.iot_http_integration.FileCategory;
import com.envisioniot.enos.iot_http_integration.HttpConnection;
import com.envisioniot.enos.iot_http_integration.message.IFileCallback;
import com.envisioniot.enos.iot_mqtt_sdk.core.exception.EnvisionException;
import com.envisioniot.enos.sdk.data.DeviceInfo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DownloadFileSample {
    // EnOS API Gateway URL and HTTP Integration Channel URL, which can be obtained from Environment Information page in EnOS Console
    static final String API_GW_URL = "http://api_gw_url";
    static final String INTEGRATION_CHANNEL_URL = "http://integration_channel_url";

    // EnOS Application AccessKey and SecretKey, which can be obtain in Application Registration page in EnOS Console
    static final String APP_KEY = "appKey";
    static final String APP_SECRET = "appSecret";

    // Device credentials, which can be obtained from Device Details page in EnOS Console
    static final String ORG_ID = "orgId";
    static final String ASSET_ID = "assetId";
    static final String PRODUCT_KEY = "productKey";
    static final String DEVICE_KEY = "deviceKey";

    public static void main(String[] args) {
        // Construct a http connection
        HttpConnection connection = new HttpConnection.Builder(
                INTEGRATION_CHANNEL_URL, API_GW_URL, APP_KEY, APP_SECRET, ORG_ID)
            .build();

        DeviceInfo deviceInfo = new DeviceInfo().setAssetId(ASSET_ID);
        // fileUri is an enos scheme file uri
        String fileUri = "enos-connect://xxx.txt";

        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            InputStream inputStream = connection.downloadFile(deviceInfo, fileUri, FileCategory.FEATURE);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            byte[] data = outputStream.toByteArray();
            System.out.println(new String(data));
        } catch (EnvisionException | IOException e) {
            e.printStackTrace();
        }

        // Asynchronously call the file download request
        try {
            connection.downloadFile(deviceInfo, fileUri, FileCategory.FEATURE, new IFileCallback() {
                @Override
                public void onResponse(InputStream inputStream) throws IOException {
                    System.out.println("download file asynchronously");
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, len);
                    }
                    byte[] data = outputStream.toByteArray();
                    System.out.println(new String(data));
                }

                @Override
                public void onFailure(Exception failure) {
                    failure.printStackTrace();
                }
            });
        } catch (EnvisionException e) {
            e.printStackTrace();
        }
    }
}