File upload

Updated at:
Copy as MD

The file upload feature in the Android Link SDK uses the HTTP/2 streaming protocol. This topic describes how to upload files to IoT Platform.

Prerequisites

The Environment Requirements and Configuration has been downloaded.

Background information

The file upload feature:
  • Supports two upload modes:
    • Create a file to upload.
    • Overwriting an existing file.
  • Supports specifying an upload length and resuming uploads. To improve bandwidth utilization, you can allocate the size of the file to upload based on the part_len network bandwidth parameter.
Important If a device requires the file upload feature, do not use the dynamic registration (no whitelist) method for device authentication.
This topic uses H2FileManagerActivity.java from the Android Link SDK as an example to demonstrate how to use the file upload feature.
Note The H2FileManagerActivity.java file is in the src/main/java/com/aliyun/alink/devicesdk/demo/ directory.

Initialization

IStreamSender is the interface class for the HTTP/2 stream channel.

Important In the following code, replace cn-shanghai in the `endPoint` parameter with your actual region ID. For a list of Region IDs, see Regions and zones.
LinkKitInitParams params = new LinkKitInitParams();
params.deviceInfo = deviceInfo; // See the demo.
// params.propertyValues = propertyValues; // Other initialization parameters.
// params.connectConfig = userData;
// H2 initialization parameters.
IoTH2Config ioTH2Config = new IoTH2Config();
ioTH2Config.clientId = "client-id";
// Replace {pk} with your actual ProductKey.
// ioTH2Config.endPoint = "https://{pk}.iot-as-http2.cn-shanghai.aliyuncs.com";
params.iotH2InitParams = ioTH2Config;
// Initialize Link SDK.
// After Link SDK is initialized, get the IStreamSender instance as follows.
IStreamSender client = LinkKit.getInstance().getH2StreamClient();
            

Establish a connection

CompletableListener is a generic asynchronous callback interface.

// A connection or network-related exception may be thrown.
// client is the IStreamSender implementation instance obtained during initialization.
client = LinkKit.getInstance().getH2StreamClient();
client.connect(new CompletableListener<Object>() {

    @Override
    public void complete(Object o) {
        // Connection successful.
    }

    @Override
    public void completeExceptionally(Throwable throwable) {
        // Connection failed.
    }
});
            

Non-Resumable Upload

For more information about file upload types, see IANA Media Types.

final Http2Request request = new Http2Request();
// The name of the file to be stored in OSS. The file name must match the regular expression: [a-zA-Z][a-zA-Z0-9_.]*
request.getHeaders().add("x-file-name", "fileName");
// Specifies whether to overwrite a file with the same name. 0: do not overwrite. 1: overwrite. The default is 0. If the file exists and you specify not to overwrite it, the stream creation fails.
request.getHeaders().add("x-file-overwrite", "1");
// The file type. If not specified, OSS automatically determines the type.
// request.getHeaders().add("x-file-content-type", "jpg");

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

client.uploadFile(serviceName, request, filePath, new CompletableDataListener<Http2Response>() {
    @Override
    public void complete(Http2Response http2Response) {
        // Upload successful.
    }

    @Override
    public void completeExceptionally(Throwable throwable) {
        // Upload failed.
    }

    @Override
    public void callBack(String fileUploadID) {
        // The ID of the current upload. To resume the upload later, you must pass this fileUploadID.
        // The callback may be invoked multiple times. Save the last fileUploadID for a future resumable upload.
    }
});
            

Resumable upload

If a previous file upload failed and you need to resume it, pass the `fileUploadId` from the previous attempt.

final Http2Request request = new Http2Request();
// fileUploadId is the ID from the previous incomplete upload.
request.getHeaders().add("x-file-upload-id", fileUploadId);
String serviceName = "/c/iot/sys/thing/file/upload";
// Replace this with the actual path of the file to upload.
String filePath = "/sdcard/demo.jpg";

client.uploadFile(serviceName, 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;
    }
});
            

Disconnect

Disconnect after the file upload is complete.

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

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