File upload

Updated at:

This topic describes how to use Link SDK for Android to upload files to Alibaba Cloud IoT Platform by using the HTTP/2 streaming protocol.

Overview

  • The SDK for Android supports multiple upload methods such as upload by creating a file or overwriting an existing file.
  • The SDK for Android allows devices to upload file data of a specified size and continue to upload the content that exceeds the size in subsequent uploads. You can set the size (part_len) based on the network bandwidth to improve the bandwidth utilization.

This section uses src/main/java/... /H2FileManagerActivity.java to explain how to upload a file by using the SDK for Android.

Perform initialization

IStreamSender is an API operation class for the HTTP/2 streaming channel.

LinkKitInitParams params = new LinkKitInitParams();
params.deviceInfo = deviceInfo; // See the demo.
// params.propertyValues = propertyValues; // Other initialization parameters.
// params.connectConfig = userData;
// The H2 initialization parameter.
IoTH2Config ioTH2Config = new IoTH2Config();
ioTH2Config.clientId = "client-id";
// Set the endpoint for your instance as follows:
ioTH2Config.endPoint = "https://" + productKey + "-x.iot-as-http2.{region_id}.aliyuncs.com:443"; // Production environment. Replace {region_id} with the Region ID of your instance.
params.iotH2InitParams = ioTH2Config;
// Perform the SDK initialization.
// After the SDK is initialized, you can obtain the IStreamSender instance by using the following method:
IStreamSender client = LinkKit.getInstance().getH2StreamClient();
            

Create a connection

CompletableListener is a universal asynchronous callback interface.

// An exception that indicates a created connection or a network error may be returned.
// The client parameter indicates the IStreamSender instance that is obtained during the initialization.
client = LinkKit.getInstance().getH2StreamClient();
client.connect(new CompletableListener<Object>() {

    @Override
    public void complete(Object o) {
        // The connection is created.
    }

    @Override
    public void completeExceptionally(Throwable throwable) {
        // The connection failed to be created.
    }
});
            

Upload files

Non-resumable upload

For more information about the file types that can be uploaded, see IANA Media Types.

final Http2Request request = new Http2Request();
// The name of a file that is stored in an Object Storage Service (OSS) bucket. The regular expression for the file name verification rule is [a-zA-Z][a-zA-Z0-9_.] *.
request.getHeaders().add("x-file-name", "fileName");
// Specify whether to overwrite a file with the same name. Valid values: 0 (not overwrite) and 1 (overwrite). Default value: 0. If the file already exists and the default value (0) is used, the stream fails to be created.
request.getHeaders().add("x-file-overwrite", "1");
// The type of the file to be uploaded. If this parameter is not set, OSS automatically assigns a value.
// request.getHeaders().add("x-file-content-type", "jpg");

String serviceName = "/c/iot/sys/thing/file/upload";
// Replace the path with the path of the file to be uploaded.
String filePath = "/sdcard/demo.jpg";

client.uploadFile(serviceNme, request, filePath, new CompletableDataListener<Http2Response>() {
    @Override
    public void complete(Http2Response http2Response) {
        // The file is uploaded.
    }

    @Override
    public void completeExceptionally(Throwable throwable) {
        // The file failed to be uploaded.
    }

    @Override
    public void callBack(String fileUploadID) {
        // The ID of the upload. If you need resumable upload, specify the fileUploadID parameter.
        // If multiple callbacks exist, store the latest value of the fileUploadID parameter for resumable upload later.
    }
});
            

Resumable upload

If you need to resume the upload of a file that fails to be uploaded, you must pass in the latest value of the fileUploadId parameter.

final Http2Request request = new Http2Request();
// The fileUploadId parameter specifies the ID of the last unfinished upload.
request.getHeaders().add("x-file-upload-id", fileUploadId);
String serviceName = "/c/iot/sys/thing/file/upload";
// Replace the path with the path of the file to be uploaded.
String filePath = "/sdcard/demo.jpg";

client.uploadFile(serviceNme, request, filePath, new CompletableDataListener<Http2Response>() {
    @Override
    public void complete(Http2Response http2Response) {
        showToast(http2Response.toString() + "success");
    }

    @Override
    public void completeExceptionally(Throwable throwable) {
        showToast(throwable.toString() + "fail");
    }

    @Override
    public void callBack(String fileUploadID) {
        fileUploadId = fileUploadID;
    }
});
            

Disconnection

The stream connection between the device and IoT Platform breaks after a file is uploaded.

// The client parameter indicates the IStreamSender instance that is obtained during the initialization.
client.disconnect(new CompletableListener() {
    @Override
    public void complete(Object o) {
    }

    @Override
    public void completeExceptionally(Throwable throwable) {
    }
});