· 1 分钟阅读时长 · 170 字 · -阅读 -评论

title: “Resumable Transfers with ssh2-sftp-client” tags:

  • WebShell slug: ‘17038235’ date: 2025-03-21 10:46:13

I’ve used ssh2-sftp-client to implement terminal-side upload/download. Because it supports streams, I implemented chunked uploads/downloads with progress bars. But networks are unreliable and transfers can be interrupted, so resumable transfer is needed. Looking into the implementation, I found that ssh2-sftp-client can support resuming.

put/get Methods

The third parameter to put/get is options. It includes writeStreamOptions/readStreamOptions. Inspecting their definitions shows a start parameter, which indicates the byte offset to start from.

With start, resumable transfer becomes straightforward.

export interface ReadStreamOptions extends ReadableOptions {
    flags?: OpenMode;
    mode?: number;
    start?: number;
    end?: number;
    autoClose?: boolean;
    handle?: Buffer;
}

export interface WriteStreamOptions extends WritableOptions {
    flags?: OpenMode;
    mode?: number;
    start?: number;
    autoClose?: boolean;
    handle?: Buffer;
    encoding?: BufferEncoding;
}

Conceptual Implementation

Take upload as an example. After a successful transfer segment, record the last transferred byte offset on the client. If the network drops, the next attempt sets start to that offset and continues. In essence, resumable transfer is similar to chunked transfer.

Alan H
Authors
开发者,数码产品爱好者,喜欢折腾,喜欢分享,喜欢开源